Ruby on Rails 3: link_to create new nested resource? -


i trying make link create new nested resource in rails 3 application, can't figure out. syntax link new nested resource

solution:

make sure have resources nested in routes file.

resources :books   resources :chapters end 

then in view script can call this:

<%= link_to 'new chapter', new_book_chapter_path(@book) %> 

the rails guide on routing quite helpful.

note: if message couldn't find book without id, problem isn't link, it's code in controller.

def new   @book = book.find(params[:book_id]) #instead of :id   @chapter = @book.chapter.new   respond_with(@chapter) end 

make changes in routes as

map.resources :books |book|     book.resources :chapters end 

and use this

link_to new_book_chapter_path(@book) 

you can use link understand concept better nested routes


Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -