【问题标题】:wrong number of arg for devise emails设计电子邮件的 arg 数量错误
【发布时间】:2013-02-23 13:04:22
【问题描述】:

我以前没有经历过这种情况,但是自从将我的 rails 站点移至 Heroku 后,每当尝试触发 Devise 发送电子邮件时,我都会收到以下消息

Started POST "/members/forgot-password" for 127.0.0.1 at 2013-02-24 00:02:27 +1100
Processing by Devise::PasswordsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"9G1P34ddbq2TN7SkmFuCet5d7fPMvWdSSpIaGqSZW9g=", "user"=>{"email"=>"paul.mcguane@*****"}, "commit"=>"Recover password"}
  User Load (3.1ms)  SELECT "users".* FROM "users" WHERE "users"."email" = 'paul.mcguane@me.com' LIMIT 1
Completed 500 Internal Server Error in 31ms

ArgumentError - wrong number of arguments (2 for 1):
  app/mailers/devise/mailer.rb:8:in `reset_password_instructions'

mailer.rb

    class Devise::Mailer < ::ActionMailer::Base
  include Devise::Mailers::Helpers

  def confirmation_instructions(record)
    devise_mail(record, :confirmation_instructions)
  end

  def reset_password_instructions(record)
    devise_mail(record, :reset_password_instructions)
  end

  def unlock_instructions(record)
    devise_mail(record, :unlock_instructions)
  end
end

【问题讨论】:

  • 您正在运行哪个版本的 Rails / Devise,以及您的应用程序是否有任何其他依赖项来处理 Heroku 上的电子邮件传递?
  • 最终卸载了 gem,重新安装似乎可以解决它:S

标签: ruby-on-rails devise actionmailer


【解决方案1】:

latest version 似乎需要一个额外的参数。
至少它适用于我的代码。这是我的自定义邮件程序类的工作代码。
注意每个方法的额外参数action

  class MyMailer < ActionMailer::Base

    include Devise::Mailers::Helpers

    def confirmation_instructions(record, token, opts={})
      @token = token
      devise_mail(record, :confirmation_instructions, opts)
    end

    def reset_password_instructions(record, token, opts={})
      @token = token
      devise_mail(record, :reset_password_instructions, opts)
    end

    def unlock_instructions(record, token, opts={})
      @token = token
      devise_mail(record, :unlock_instructions, opts)
    end

    # optional: this override method is from Devise::Mailers::Helpers
    def headers_for(action,opts={})
      …
    end

  end

【讨论】:

  • 在哪里添加这个文件?
【解决方案2】:

尝试这种方式,因为上面的答案中提到“设计在最新版本中引入额外的选项参数”

     def reset_password_instructions(record, opts={})
       devise_mail(record, :reset_password_instructions)
     end

【讨论】:

    【解决方案3】:

    这是由于 Devise 在最近的版本中引入了一个额外的选项参数。我想你想要这样的东西:

    class Devise::Mailer < ::ActionMailer::Base
      include Devise::Mailers::Helpers
    
      def confirmation_instructions(record, opts={})
        devise_mail(record, :confirmation_instructions, opts={})
      end
    
      def reset_password_instructions(record)
        devise_mail(record, :reset_password_instructions, opts={})
      end
    
      def unlock_instructions(record)
        devise_mail(record, :unlock_instructions, opts={})
      end
    
      def headers_for(actions, opts={}
        # see http://stackoverflow.com/a/14698599/18706
      end
    
    end
    

    【讨论】:

    • 我应该在哪里添加这个文件?
    • app/mailers/devise/mailer.rb 或类似
    • 那条路径直接来自 OP 问题的输出。我在 app/mailers/user_mailer.rb 中将它作为 UserMailer。确保您的邮件类以 include Devise::Mailers::Helpers 开头。
    • 并注意 Webdevotion 的回答指出了这个额外的令牌 arg。
    【解决方案4】:

    最终卸载了 gem,重新安装似乎可以解决它:S

    【讨论】:

      猜你喜欢
      • 2013-05-14
      • 2013-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多