You forgot to specify the book in the edit
action of your controller, and since you need it in several actions of your controller, you could create a before_filter
callback to make your code more DRY.
For example:
class SnippetsController < ApplicationController
before_filter :authenticate_user!, only: [:create]
before_filter :find_book
def create
@snippet = @book.snippets.create!(params[:snippet])
redirect_to @book
end
def edit
@snippet = Snippet.find(params[:id])
end
private
def find_book
@book = Book.find(params[:book_id])
end
end
I removed the show
action of the controller because it should be used to display one snippet, not all the snippets of a book. To do so, you can use the index
action of the SnippetsController
, or better, the show
action of the BooksController
.
One last thing, the url you use in the form declaration is wrong because you need to specify both the book and the snippet:
<%= form_for([@book, @snippet], :url => edit_book_snippet_path(@book, @snippet)) do |f| %>
To get the basis of a blog creation with Rails, I suggest you to read the classic Rails guide.