当我使用设计 gem 进行注册/登录时,如何使用代码创建用户



我正在为我的应用程序使用 Devise gem。现在我想使用代码创建一个用户,但在数据库表中"用户"密码已加密。所以我希望我不能直接将其保存为

 
 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
 

这可能吗?

实际上,devise的encrypted_password方法将处理加密您传入的密码并将其存储在数据库中

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

在 rails 控制台中试用一下。

最新更新