Ruby on Rails with Sorcery 重置密码电子邮件出现未定义方法错误

发布于 2024-12-19 16:17:36 字数 1950 浏览 2 评论 0原文

使用 Sorcery 0.7.4 和 Rails 3.1.1 进行身份验证。 一切都很顺利,直到我尝试设置密码重置。

激活工作正常并且电子邮件已发送,但由于某种原因,我在尝试发送重置密码电子邮件时收到此错误。

undefined method `reset_password_email' for nil:NilClass

我完全复制了教程,当我在控制台中进行快速测试时,它按预期发出了电子邮件。在控制台中:

user = User.find(1)
user.deliver_reset_password_instructions!

在实际控制器中,它通过从表单提交的电子邮件找到用户,在日志中我可以看到它正在检索正确的用户并设置令牌,但出现如上所述的错误并回滚。

我检查了 gem 的 Deliver_reset_password_instructions 代码!而且似乎没有理由失败。

PasswordResetsController:

@user = User.find_by_email(params[:email])
@user.deliver_reset_password_instructions! if @user

以下内容是从 gem 代码复制的:

Instance Method in Gem:

def deliver_reset_password_instructions!
    config = sorcery_config
    # hammering protection
    return false if config.reset_password_time_between_emails && self.send(config.reset_password_email_sent_at_attribute_name) && self.send(config.reset_password_email_sent_at_attribute_name) > config.reset_password_time_between_emails.ago.utc
    self.send(:"#{config.reset_password_token_attribute_name}=", TemporaryToken.generate_random_token)
    self.send(:"#{config.reset_password_token_expires_at_attribute_name}=", Time.now.in_time_zone + config.reset_password_expiration_period) if config.reset_password_expiration_period
    self.send(:"#{config.reset_password_email_sent_at_attribute_name}=", Time.now.in_time_zone)
    self.class.transaction do
        self.save!(:validate => false)
        generic_send_email(:reset_password_email_method_name, :reset_password_mailer)
    end
end


The method called above for mailing:

def generic_send_email(method, mailer)
    config = sorcery_config
    mail = config.send(mailer).send(config.send(method),self)
    if defined?(ActionMailer) and config.send(mailer).superclass == ActionMailer::Base
        mail.deliver
    end
end

同样,所有必需的邮件程序片段都在那里,并且可以从控制台工作。

Using Sorcery 0.7.4 with Rails 3.1.1 for authentication.
Everything was going well until I tried to setup password resetting.

Activation works perfectly and emails are sent, but for some reason I get this error when trying to send the reset password email.

undefined method `reset_password_email' for nil:NilClass

I copied the tutorial exactly, and when I did a quick test in the console it shot off the email as expected. In console:

user = User.find(1)
user.deliver_reset_password_instructions!

In the actual controller, it finds the user by the email submitted from the form and in the log I can see it is retrieving the right user and setting the token, but errors out as above and rolls back.

I checked the gem's code for deliver_reset_password_instructions! and there seems to be no reason for it to fail.

PasswordResetsController:

@user = User.find_by_email(params[:email])
@user.deliver_reset_password_instructions! if @user

The following is copied from the gem code:

Instance Method in Gem:

def deliver_reset_password_instructions!
    config = sorcery_config
    # hammering protection
    return false if config.reset_password_time_between_emails && self.send(config.reset_password_email_sent_at_attribute_name) && self.send(config.reset_password_email_sent_at_attribute_name) > config.reset_password_time_between_emails.ago.utc
    self.send(:"#{config.reset_password_token_attribute_name}=", TemporaryToken.generate_random_token)
    self.send(:"#{config.reset_password_token_expires_at_attribute_name}=", Time.now.in_time_zone + config.reset_password_expiration_period) if config.reset_password_expiration_period
    self.send(:"#{config.reset_password_email_sent_at_attribute_name}=", Time.now.in_time_zone)
    self.class.transaction do
        self.save!(:validate => false)
        generic_send_email(:reset_password_email_method_name, :reset_password_mailer)
    end
end


The method called above for mailing:

def generic_send_email(method, mailer)
    config = sorcery_config
    mail = config.send(mailer).send(config.send(method),self)
    if defined?(ActionMailer) and config.send(mailer).superclass == ActionMailer::Base
        mail.deliver
    end
end

Again all the required mailer bits and pieces are there and work from the console.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

执妄 2024-12-26 16:17:36

在魔法初始值设定项中取消注释此行

user.reset_password_mailer = UserMailer  
user.reset_password_email_method_name = :reset_password_email

Uncomment this lines in the sorcery initializer

user.reset_password_mailer = UserMailer  
user.reset_password_email_method_name = :reset_password_email
暮光沉寂 2024-12-26 16:17:36

检查您的 app/mailers/user_mailer.rb 文件。

如果您遵循本教程,您可能会执行类似将 wiki 中的方法定义(采用一个参数)复制并粘贴到生成的方法定义(不采用任何参数)中的操作,因此会出现 1 for 0 ArgumentError。

换句话说,您可能会遇到如下所示的情况:

def reset_password_email
   def reset_password_email(user)

这很糟糕,但很容易解决:-)

Check your app/mailers/user_mailer.rb file.

If you were following the tutorial you probably did something like copy and paste the method definition from the wiki (which takes one parameter) into the generated method definition (which doesn't take any parameter), hence the 1 for 0 ArgumentError.

In other words, you likely have something that looks like this:

def reset_password_email
   def reset_password_email(user)

This is bad, but an easy fix :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文