ruby-on-railsbcrypt-ruby

How to update password attribute with has_secure_password?


I used has_secure_password for the User model. Now I am trying to use AJAX to update some of the user's attributes, including password. However, it looks like with has_secure_password, the password attribute no longer exists, replaced by password_digest. So when I am trying to do

user[:password] = "The password passed by AJAX"
user.save!

I got:

ActiveModel::MissingAttributeError (can't write unknown attribute password)

The question is: What is the right way to update a user's password in this situation? Do I need to manually compute the hash and update the password_digest?

EDIT:

I am using Rails 4.2.1


Solution

  • Normally you just use:

    user = User.find 1
    user.password = 'Test123456789'
    user.save
    

    But, it sounds like you have not added a password_digest column to the users table or have not run the migrations.