I am trying to make a link to create a new nested resource in my Rails 3 application, but I can't figure it out. What is the syntax to link to a new nested resource
Solution:
Make sure you have your resources properly nested in your routes file.
resources :books do
resources :chapters
end
Then in your view script you can call it like this:
<%= link_to 'New Chapter', new_book_chapter_path(@book) %>
The Rails Guide on Routing was quite helpful.
Note: if you get a message like Couldn't find Book without an ID
, the problem isn't the link, it's the code in your controller.
def new
@book = Book.find(params[:book_id]) #instead of :id
@chapter = @book.chapter.new
respond_with(@chapter)
end