I’m having problems trying to edit a model. I always end up getting as error
undefined method `model_name' for NilClass:Class
I’m using in the view:
<%= form_for(@book) do |f| %>
and on the controller:
def edit
@title = "Edit Book"
end
def update
@book = Book.find(params[:id])
if @book.update_attributes(params[:book])
flash[:success] = "Book Updated"
redirect_to @book
else
@title = "Edit Book"
render 'edit_book_path(@book)'
end
end
You have to initialize the @book variable in the ‘edit’ action. As you can see from your view:
the @book variable is used for rendering the form. Thus:
is needed in edit method.