【问题标题】:How to re-generate the user.confirmation_token如何重新生成 user.confirmation_token
【发布时间】:2014-01-13 23:25:51
【问题描述】:

我正在寻找一种方法来重新生成 user.confirmation_token。在我的应用程序中,我允许用户使用 user.deleted_at 停用他们的帐户。如果停用的用户尝试重新激活他们的帐户,我想查看确认邮件,但需要设置确认令牌。有没有办法在 RegistrationsController 中重新生成 user.confirmation_token?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 devise


    【解决方案1】:

    Confirmable 模块中的generate_confirmation_token 方法通过ActiveSupport::Concern 应用于其他类。就像你在这里看到的:

    https://github.com/plataformatec/devise/blob/master/lib/devise/models/confirmable.rb

    在您的用户对象上重新应用此方法应该可以解决问题。

    // generate_confirmation_token 方法受到保护,因此不能从类外部调用。您可以运行send_confirmation_instructions,它会生成令牌并发送注册邮件。

    // 如果 gem 的 confirmable 模块让它看起来像方法一样,则在源代码中查看。如果默认情况下不可能,则需要对 gem 进行定制。您可以在bash中通过以下命令获取gemsets路径

    rvm gemset path
    

    然后在gems 目录中,您将找到已安装的设计版本。在lib/devise/models/confirmable.rb 文件中,您可以添加调用generate_confirmation_token 方法的不受保护的方法。您只能从该模块调用它,因为它是受保护的。

    【讨论】:

    • 我无法让它工作。我将如何在registrations_controller 中应用/使用它?谢谢
    • 如果你只运行user.generate_confirmation_token会发生什么?
    • 这是错误:NoMethodError (protected method `generate_confirmation_token!' called for #<0x007fc9b5c880a8>
    猜你喜欢
    • 2015-04-03
    • 2014-07-30
    • 2011-01-03
    • 2020-01-07
    • 2019-02-07
    • 2017-01-04
    • 1970-01-01
    • 1970-01-01
    • 2012-12-13
    相关资源
    最近更新 更多