Reputation: 5603
I have the following in my edit page for my persons
view:
<%= form_for @person do |f| %>
<%= label_tag :city %>
<%= f.text_field :city %>
<%= label_tag :state %>
<%= f.text_field :state %>
<%= label_tag :name %>
<%= f.text_field :name %>
<%= f.submit %>
<% end %>
The idea is that I could update the :city
, :state
, and :name
attributes on my @person
model from this view.
What does my controller need to look like to make this happen? My route is passing through correctly, and in my controller I have this so far:
def update
@person = Person.find(params[:id])
@person.update_attributes!(@person_params)
redirect_to @person
end
However, this just redirects my back to my show page for @person each time without actually updating the attributes on the model. What am I missing here?
Upvotes: 0
Views: 1783
Reputation: 427
You could do something like this in your controller update action.
Passing the Person attributes as strong params. This is the preferred method, as using strong params is recommended.
def update
@person = Person.find(params[:id])
@person.update(person_params)
redirect_to @person
end
Make it a private method as:
private
def person_params
params.require(:person).permit(:city, :state, :name)
end
Upvotes: 0
Reputation: 6981
Should look something like this:
def update
@person = Person.find(params[:id])
respond_to do |format|
if @peron.update_attributes(params[:person])
format.html { redirect_to people_url, notice: "#{@person.name} was updated." }
else
format.html { render action: "edit" }
end
end
end
Upvotes: 2