The problem of keeping rails
I have a Ruby on Rails page to edit a user profile. The form displays the username and email address correctly. When I change the name in the text box and click update, I am returned to the edit_profile page with the message "Profile updated successfully". The problem is that the value I changed was not saved in the database.
There are no errors and the parameters on the server output look correct.
Handling updates UserController # (for 127.0.0.1 at 2009-05-19 22:00:48) [PUT] Parameters: {"user" => {"name" => "new name", "Email" => " test@test.com "}," Commit "=>" Update "," action "=>" update "," _method "=>" put "," Authenticity_token "=>" 59c79fa90aaf5558aaab8cddef6acb7a4c7c55c3 "," id "=>" 1 "," controller "=>" users "}
What am I missing?
edit_profile.html.erb
<% form_for @profile, :url => {:action => "update", :id => @profile} do |f| %>
<%= f.error_messages %>
<p>Name: <%= f.text_field :name %></p>
<p>Email: <%= f.text_field :email %></p>
<%= f.submit "Update" %>
<% end %>
users_controller.rb
def edit_profile
@profile = User.find(current_user.id)
end
def update
@profile = User.find(params[:id])
respond_to do |format|
if @profile.update_attributes(params[:profile])
flash[:notice] = 'Profile was successfully updated.'
format.html { render :action => 'edit_profile' }
else
flash[:notice] = 'Profile Error.'
format.html { render :action => "edit_profile" }
end
end
end
EDIT: Yes, it was a naming issue, to fix this I changed ...
if @profile.update_attributes(params[:profile])
to
if @profile.update_attributes(params[:user])
a source to share
Your field names do not match what you pass to the update_attributes method.
Check form field names (using firebug) and there will be "name" etc.
But you are passing an array called "params":
update_attributes(params[:profile])
Form fields must be named "profile [name]" to work correctly.
If you check development.log, you can see that no update is being called.
a source to share