I have a form that I am using to 'edit' existing teams. Here is the view:
/app/views/teams/edit.html.erb
<%= semantic_form_for @team do |f| %>
<%= f.inputs %>
<%= f.actions %>
<% end %>
And here is the controller's code:
/app/controllers/teams_controller.rb
def edit
@team = Team.find(params[:id])
end
def update
if @team.update_attributes(params[:team])
redirect_to @team
else
render 'edit'
end
end
However, when I go to /teams/1/edit and change the name of the team and click Update to submit the form, it gives the error: undefined method `update_attributes' for nil:NilClass
At the beginning of the update method in the controller, I did a:
raise @team.inspect
and my suspicion was proved correct when I saw that @team was inspected in the update method it was nil (i.e. it's value was not passed).
Does anyone know where I am going wrong here?
Thank you.
The problem is that you need to define @team in the update action before you can use it. Simply add this line to the beginning of the action:
@team = Team.find(params[:id])