views:

81

answers:

1

I have a Ruby on Rails page to edit a users profile. The form displays the user name and email correctly. When I change the name in the text box and click update I end up back at the edit_profile page with the message 'Profile was successfully updated.'. The problem is the value I changed the name to did not save to the database.

There are no errors and the parameters in the server output look correct.

Processing UsersController#update (for 127.0.0.1 at 2009-05-19 22:00:48) [PUT] Parameters: {"user"=>{"name"=>"new name", "email"=>"[email protected]"}, "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 this was was a naming issue, to fix this I changed ...

if @profile.update_attributes(params[:profile])

to

if @profile.update_attributes(params[:user])
+1  A: 

The names of your fields do not match what you are passing to the update_attributes method.

Check the names of the form fields (using firebug), and the will be "name" etc.

But you are passing an array called "params":

update_attributes(params[:profile])

Your form fields should be called "profile[name]" for this to work properly.

If you check the development.log you should see that no update is ever called.

Toby Hede
Yes you are exactly right. Thanks Toby.
Mark Robinson