rails update_attributes returns false when trying to update db values
Asked Answered
S

2

18

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!

Sorbose answered 17/4, 2012 at 10:4 Comment(3)
Have you checked development.log?Chancellorsville
A description of the error message would be nice - don't know how you're just getting falseEdgaredgard
added development.log output in the main post.... (just getting a rollback)Sorbose
I
47

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
Incommode answered 17/4, 2012 at 10:16 Comment(3)
That's it! That's exactly what I needed! The error log revealed I was missing a field that needed to be updated along with these. Thanks very much, now I know how to properly debug these errors too! :-)Sorbose
I am facing similar problem now, I tried Rails.logger.info(@user.errors.messages.inspect) and it returns me "True" . What does it mean? I have to update a column and I have added the column in attr_accessible. while updating it returns me false and query it is executing is also weird. Have a look here paste.lisp.org/+2S4Y I cant update the fields. what may be the problem?Crapulous
@JyotiRanjan check @user.errors.messages.inspect it works at least in my case with Rails 4.2Celestina
C
9

You can also get better introspection when updating attributes by calling update_attributes! instead, which raises exceptions instead of just returning false.

Chavannes answered 17/8, 2015 at 20:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.