简体   繁体   中英

How to create a user using code when i am using devise gem for Sign up / Sign in

I am using Devise gem for my application. Now i would like to create a user using code, but in the database table "users" the password is encrypted. So i hope i cannot directly save it as


 new_user = User.new
 new_user.email = "xyz@xys.com"
 new_user.password = "1sdf" - i cannot use this becs its actually : encrypted_password
 new_user.save
 

Is this possible?

You should be able to do this actually, devise's encrypted_password method will handle encrypting the password you pass in and storing it in the database

u = User.new(:email => "foo@bar.com", :password => '1sdf', :password_confirmation =>       '1sdf')
u.save

Try it out in the rails console.

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