rails update_attributes returns false when trying to update db values

steve picture steve · Apr 17, 2012 · Viewed 24.9k times · Source

hoping someone here can point me in the right direction.

I have a controller Update def running "update_attributes". Currently it returns false, with no error message. I'm fairly new to Ruby, but not to coding, and this has had me stumped for a good few days! I am trying to get the User model and db updated with the values specified below.

def update   
    #get currently logged in user
    @user = current_user
    #update user params based on edit form...
    if @user.update_attributes(params[:user])
        redirect_to profile_path, :notice  => "Successfully updated profile."
    else
        render :action => 'edit'
    end
end

my edit def....

def edit
    @user = current_user
end

The form sends the following to this update method:

--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess
utf8: ✓
_method: put
authenticity_token: 9T4ihVI0p8j7pZEFxof3Bfahi2a+o3BPmtXJDnfHT4o=
user: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
  first_name: Amanda
  last_name: Ross
  is_owner: '1'
  email: [email protected]
commit: Update
action: update
controller: users
id: '20'

And params[:user] returns:

{"first_name"=>"Amanda", "last_name"=>"Ross", "is_owner"=>"1", "email"=>"[email protected]"}

All these fields are in attr_accessible in the Model, and I can create a user without any problem.

here's development.log output (sorry it's a bit messy)....

Started PUT "/users/20" for 127.0.0.1 at 2012-04-17 10:39:29 +0100
Processing by UsersController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"9T4ihVI0p8j7pZEFxof3Bfahi2a+o3BPmtXJDnfHT4o=", "user"=>    {"first_name"=>"Amanda", "last_name"=>"Ross", "is_owner"=>"1", "email"=>"[email protected]"},     "commit"=>"Update", "id"=>"20"}
  [1m[36mUser Load (1.0ms)[0m  [1mSELECT "users".* FROM "users" WHERE "users"."id" = 20 LIMIT 1[0m
>>>>>>>>>>>>
{"first_name"=>"Amanda", "last_name"=>"Ross", "is_owner"=>"1", "email"=>"[email protected]"}
     [1m[35m (0.0ms)[0m  begin transaction
     [1m[36mUser Exists (0.0ms)[0m  [1mSELECT 1 FROM "users" WHERE (LOWER("users"."email") =     LOWER('[email protected]') AND "users"."id" != 20) LIMIT 1[0m
  [1m[35mUser Exists (0.0ms)[0m  SELECT 1 FROM "users" WHERE ("users"."email" = '[email protected]'     AND "users"."id" != 20) LIMIT 1
  [1m[36m (0.0ms)[0m  [1mrollback transaction[0m
 Rendered users/_form.html.erb (7.0ms)
 Rendered users/edit.html.erb within layouts/application (10.0ms)
 Rendered layouts/_includes.html.erb (30.0ms)

Could anyone possibly help point out where I'm going wrong, please?

Thanks in advance!

Answer

yfeldblum picture yfeldblum · Apr 17, 2012

Both #update_attributes and #save will first check if your model instance is #valid? before continuing on to write to the database. If your model instance is not #valid?, then these methods will return false. To see what's wrong with your model instance, take a look at your model's #errors.

Rails.logger.info(@user.errors.messages.inspect)

Or, embedded in your method,

def update   
  @user = current_user
  if @user.update_attributes(params[:user])
    redirect_to profile_path, :notice  => "Successfully updated profile."
  else
    # print the errors to the development log
    Rails.logger.info(@user.errors.messages.inspect)
    render :action => 'edit'
  end
end