简体   繁体   中英

How do I manually change/update a user password in devise 3.2.x?

I tried to do this from my seeds.rb :

user = User.find_or_create_by email: ENV['ADMIN_EMAIL'].dup, password: ENV['ADMIN_PASSWORD'].dup, password_confirmation: ENV['ADMIN_PASSWORD'].dup

I am getting this error message:

PG::UndefinedColumn: ERROR:  column users.password does not exist
LINE 1: ...sers"  WHERE "users"."email" = 'abc@test.com' AND "users"."p...

I tried encrypted_password , but then I get the same for users.encrypted_password_confirmation does not exist.

Thoughts on the best way to do this?

I would just go into the Rails console ( rails c )

user = User.new(email: "myemail.com", password: "superSecret")
user.save

You can also change it

user = User.find_by_email("myemail.com")
user.password = "newSuperSecret"
user.save

EDIT:

I just put my first block of code in my seed.rb and ran rake db:seed with no problem. So I think the crux of your problem is just having the password attribute in your find_or_create_by call. I think if you put this in your seed.rb you'll get the results you want (although I have to admit I'm not certain about the environment variables working, you might have to retool that portion of it).

user = User.find_or_create_by(email: ENV['ADMIN_EMAIL'].dup)
user.password = ENV['ADMIN_PASSWORD'].dup if user.encrypted_password.blank?
user.save!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM