Ruby-on-rails 从 Rails 控制台设计密码重置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8306430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Devise password reset from Rails console
提问by ylluminate
While running an app how do you select a user by email address and then set the password manually within rails consolefor Devise?
在运行应用程序时,如何通过电子邮件地址选择用户,然后在rails consoleDevise 中手动设置密码?
Also, where would I go to review documentation to cover more details in this regard to manipulation of accounts while using Devise?
另外,在使用 Devise 时,我应该去哪里查看文档以涵盖有关操纵帐户的更多详细信息?
回答by Sergio Tulentsev
It's more or less as you described :-)
或多或少如您所描述的那样:-)
# use mongoid
class User
include Mongoid::Document
end
# then
user = User.where(email: '[email protected]').first
if user
user.password = new_password
user.password_confirmation = new_password
user.save
end
Update from 6 years later :)
6 年后更新:)
Modern devise allows simpler syntax, no need to set the confirmation field
现代设计允许更简单的语法,无需设置确认字段
user.password = new_password; user.save
# or
user.update_attributes(password: new_password)
回答by Eric Guo
# $ rails console production
u=User.where(:email => '[email protected]').first
u.password='userpassword'
u.password_confirmation='userpassword'
u.save!
回答by gstraehle
If you run the following in the rails console it should do the trick:
如果您在 rails 控制台中运行以下内容,它应该可以解决问题:
User.find_by(email: 'user_email_address').reset_password!('new_password','new_password')
http://www.rubydoc.info/github/plataformatec/devise/Devise/Models/Recoverable
http://www.rubydoc.info/github/plataformatec/devise/Devise/Models/Recoverable
回答by Kshitij
You can simply update password field, no need for confirmation password, devise will save it in encrypted form
您可以简单地更新密码字段,无需确认密码,设计会以加密形式保存
u = User.find_by_email('[email protected]')
u.update_attribute(:password, '123123')
回答by robinwen
1.Login in to ralis console
1.登录到ralis控制台
$ sudo bundle exec rails console production
2.Then update the administrator's password
2.然后更新管理员密码
irb(main):001:0> user = User.where("username = 'root'")
irb(main):002:0> u = user.first
irb(main):003:0> u.password="root2014@Robin"
=> "root2014@Robin"
irb(main):004:0> u.password_confirmation="root2014@Robin"
=> "root2014@Robin"
irb(main):005:0> u.save
=> true
irb(main):006:0> exit
3.Refresh the login page, use the new password to login, enjoy!
3.刷新登录页面,使用新密码登录,尽情享受吧!
Good Luck!
祝你好运!
回答by valk
For some reason, (Rails 2.3??)
出于某种原因,(Rails 2.3??)
user = User.where(:email => email).first
didn't work for me, but
对我不起作用,但是
user = User.find_by_email('[email protected]')
did it.
做到了。
回答by copremesis
User.find_by_email('[email protected]').update_attributes(:password => 'password')
回答by Enzio
If your account is locked from too many login attempts, you may also need to do:
如果您的帐户因登录尝试次数过多而被锁定,您可能还需要执行以下操作:
user.locked_at = ''
user.failed_attempts = '0'
user.save!

