Megha D S
Megha D S

Reputation: 61

update_attributes failing when I try to update an attribute

I have a user database as User(id: integer, username: string, created_at: datetime, updated_at: datetime, password_digest: string, admin: boolean, usermanager: boolean). I want to update an user and make him Admin, setting the admin varaible to true.

My update in the controller:

def update
@user = User.find(params[:id])
if @user.update_attributes(user_params)
  flash[:success] = "User edited"
  redirect_to current_user
else
  render users_url
end
end

The update_attributes is failing in all cases. I opened a debugger and tried to call update_attributes, it is failing there as well.

(byebug) user_params = {"admin"=>true}
{"admin"=>true}
(byebug) @user
#<User id: 1, username: "dsmegha", created_at: "2015-09-11 07:42:01", updated_at: "2015-09-13 03:25:01", password_digest: "$2a$10$BInH6J1dXHanqNIdGag6megSyrmm95AmjTEGPemNdGU...", admin: false, usermanager: nil>
(byebug) @user.update_attributes(user_params)
   (0.2ms)  begin transaction
  User Exists (0.2ms)  SELECT  1 AS one FROM "users" WHERE ("users"."username" = 'dsmegha' AND "users"."id" != 1) LIMIT 1
   (0.2ms)  rollback transaction
false

The update_attribute works for the same.

(byebug) @user.update_attribute(:admin,true)
   (0.2ms)  begin transaction
   (0.1ms)  commit transaction
true
(byebug) @user
#<User id: 1, username: "dsmegha", created_at: "2015-09-11 07:42:01", updated_at: "2015-09-13 03:25:01", password_digest: "$2a$10$BInH6J1dXHanqNIdGag6megSyrmm95AmjTEGPemNdGU...", admin: true, usermanager: nil>
(byebug)

Any idea what I am missing here?

Upvotes: 0

Views: 902

Answers (1)

Atsushi Harada
Atsushi Harada

Reputation: 339

Maybe I think current user data is invalid. please check user.errors.

example)

user.rb

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable,:confirmable,
         :recoverable, :rememberable, :trackable, :validatable, :lockable

  validates :name, presence: true
end

pry

 pry(main)> user = User.last
=> #<User:0x007fe328fe56d8
 id: 3,
 email: "[email protected]",
 name: "",
 encrypted_password: "$2a$10$I.xXo0AtAz8nuCUzOGclPe6PA//XMZtjW6fh94D1t25jb7eV9bsS.",
 current_sign_in_at: Tue, 02 Jun 2015 01:53:08 JST +09:00,
 last_sign_in_at: Tue, 02 Jun 2015 01:52:20 JST +09:00,
 current_sign_in_ip: "127.0.0.1",
 last_sign_in_ip: "127.0.0.1",
 created_at: Tue, 02 Jun 2015 01:49:40 JST +09:00,
 updated_at: Sun, 13 Sep 2015 13:16:26 JST +09:00,
 confirmation_token: nil,
 confirmed_at: Tue, 02 Jun 2015 01:52:01 JST +09:00,
 confirmation_sent_at: Tue, 02 Jun 2015 01:49:40 JST +09:00,
 unconfirmed_email: nil,
 failed_attempts: 0,
 unlock_token: nil,
 locked_at: nil,
 status: 1>

[7] pry(main)> user.update_attributes(status: 2)
   (0.1ms)
        BEGIN
   (0.2ms)
        ROLLBACK
=> false

[8] pry(main)> user.errors
=> #<ActiveModel::Errors:0x007fe328d893f8
 @base=
  #<User:0x007fe328fe56d8
   id: 3,
   email: "[email protected]",
   name: "",
   encrypted_password: "$2a$10$I.xXo0AtAz8nuCUzOGclPe6PA//XMZtjW6fh94D1t25jb7eV9bsS.",
   reset_password_token: "XWmjsEBujb2-aWd5YHYT",
   reset_password_sent_at: nil,
   remember_created_at: nil,
   sign_in_count: 2,
   current_sign_in_at: Tue, 02 Jun 2015 01:53:08 JST +09:00,
   last_sign_in_at: Tue, 02 Jun 2015 01:52:20 JST +09:00,
   current_sign_in_ip: "127.0.0.1",
   last_sign_in_ip: "127.0.0.1",
   created_at: Tue, 02 Jun 2015 01:49:40 JST +09:00,
   updated_at: Sun, 13 Sep 2015 13:16:26 JST +09:00,
   confirmation_token: nil,
   confirmed_at: Tue, 02 Jun 2015 01:52:01 JST +09:00,
   confirmation_sent_at: Tue, 02 Jun 2015 01:49:40 JST +09:00,
   unconfirmed_email: nil,
   failed_attempts: 0,
   unlock_token: nil,
   locked_at: nil,
   status: 2>,
 @messages={:name=>["can't be blank"]}>

additionally write.

View has password input field perhaps? In that case, I think you need to delete passward params when update user data.

How about this?

user.rb

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable,:confirmable,
         :recoverable, :rememberable, :trackable, :validatable, :lockable

  validates :name, presence: true

  def update_without_current_password(params, *options)
    params.delete(:current_password)

    if params[:password].blank?
      params.delete(:password)
      params.delete(:password_confirmation) if params[:password_confirmation].blank?
    end

    clean_up_passwords
    update_attributes(params, *options)
  end
end

controller

def update
  @user = User.find(params[:id])
  if @user.update_without_current_password(user_params)
    flash[:success] = "User edited"
    redirect_to current_user
  else
    render users_url
  end
end

Upvotes: 2

Related Questions