从Rails控制台设置密码重置

yll*_*ate 85 ruby ruby-on-rails rails-console devise ruby-on-rails-3

在运行应用程序时,如何通过电子邮件地址选择用户,然后在rails consoleDevise中手动设置密码?

另外,在使用Devise时,我将在哪里查看文档以涵盖有关操作帐户的更多详细信息?

Ser*_*sev 129

它或多或少就像你描述的那样:-)

# use mongoid
class User
  include Mongoid::Document
end


# then
user = User.where(email: 'joe@example.com').first

if user
  user.password = new_password
  user.password_confirmation = new_password
  user.save
end
Run Code Online (Sandbox Code Playgroud)

从6年后更新:)

现代设计允许更简单的语法,无需设置确认字段

user.password = new_password; user.save
# or
user.update_attributes(password: new_password)
Run Code Online (Sandbox Code Playgroud)


Eri*_*Guo 51

# $ rails console production
u=User.where(:email => 'usermail@gmail.com').first
u.password='userpassword'
u.password_confirmation='userpassword'
u.save!
Run Code Online (Sandbox Code Playgroud)

  • devise 是在 Rails 中烘焙的,因此使用密码确认是多余的。```User.find_by_email('joe@example.com').update_attributes(:password => 'password')``` (4认同)

gst*_*hle 23

如果您在rails控制台中运行以下操作,它应该可以解决问题:

User.find_by(email: 'user_email_address').reset_password!('new_password','new_password')
Run Code Online (Sandbox Code Playgroud)

http://www.rubydoc.info/github/plataformatec/devise/Devise/Models/Recoverable

  • 请注意,感叹号已弃用,它只是:`User.find_by(email:'user_email_address').reset_password('new_password','new_password')` (6认同)

Ksh*_*tij 5

您只需更新密码字段,无需确认密码,设计将以加密形式保存

u = User.find_by_email('user@example.com')
u.update_attribute(:password, '123123')
Run Code Online (Sandbox Code Playgroud)