【问题标题】:Argument Error (wrong number of Arguments) in Ruby on Rails duing integration of Devise and PostageApp由于 Devise 和 PostageApp 的集成,Ruby on Rails 中的参数错误(参数数量错误)
【发布时间】:2016-02-17 21:31:41
【问题描述】:

我是一个相对新手的 RoR 爱好开发者。我使用 Devise 进行用户管理,并计划使用 PostageApp 发送“忘记密码”电子邮件。 RoR 版本是 4.2.0。

我已正确配置 PostageApp。并按照 PostageApp 网站上的说明进行操作。但是,我不断收到“参数数量错误”的错误。

错误信息

2015-11-16T15:01:07.785094+00:00 app[web.1]: MyDeviseMailer#reset_password_instructions: processed outbound mail in 0.8ms
2015-11-16T15:01:07.785091+00:00 app[web.1]: 
2015-11-16T15:01:07.785415+00:00 app[web.1]: Completed 500 Internal Server Error in 597ms
2015-11-16T15:01:07.775613+00:00 app[web.1]:    (0.5ms)  BEGIN
2015-11-16T15:01:07.781596+00:00 app[web.1]:    (1.6ms)  COMMIT
2015-11-16T15:01:07.787783+00:00 app[web.1]: 
2015-11-16T15:01:07.787786+00:00 app[web.1]: ArgumentError (wrong number of arguments (3 for 1)):
2015-11-16T15:01:07.787787+00:00 app[web.1]:   app/mailers/devise/mailer.rb:13:in `reset_password_instructions'

app/mailers/devise/mailer.rb

class MyDeviseMailer < PostageApp::Mailer
include Devise::Mailers::Helpers

def confirmation_instructions(record)
# PostageApp specific elements (example):
postageapp_template 'my-signup-confirmation'
postageapp_variables :email => record.email,
:link  => confirmation_url(:confirmation_token => record.confirmation_token)

devise_mail(record, :confirmation_instructions)
end

def reset_password_instructions(record)
# PostageApp specific elements (example):
postageapp_template 'my-password-reset'
postageapp_variables :name => record.name ||= record.email,
:link => password_url(:reset_password_token => record.reset_password_token)

devise_mail(record, :reset_password_instructions)
end

def unlock_instructions(record)
# PostageApp specific elements (example):
postageapp_template 'my-unlock-instructions'
postageapp_variables :name => record.name ||= record.email,
:link => unlock_url(:unlock_token => record.unlock_token)
devise_mail(record, :unlock_instructions)
end

protected
# Ensures template subject is used instead of the default devise mailer subject.
def headers_for(action)
headers = super
headers[:subject] = ‘’
headers
end
end

config/initializers/devise.rb

Devise.setup do |config|
config.mailer = "MyDeviseMailer"
config.mailer_sender = '"Webmaster" <no-reply@mysite.com>'
require 'devise/orm/active_record'
config.case_insensitive_keys = [ :email ]
config.strip_whitespace_keys = [ :email ]
config.skip_session_storage = [:http_auth]
config.stretches = Rails.env.test? ? 1 : 10
config.reconfirmable = true
config.expire_all_remember_me_on_sign_out = true
config.password_length = 8..128
config.reset_password_within = 6.hours
config.sign_out_via = :delete
end

谢谢!

【问题讨论】:

    标签: ruby-on-rails devise arguments postageapp


    【解决方案1】:

    reset_password_instructions 实际需要 3 个时,您只传递了一个必需的参数:

    reset_password_instructions(record, token, opts = {})
    

    另外,看看THIS 的帖子

    【讨论】:

    • 另外,使用你得到的token作为参数,而不是password_url中的record.reset_password_token
    • 谢谢 Cyzanfar 和 Yogesh。非常有用的见解。
    【解决方案2】:

    当我们使用设计时,我们必须编写下面的代码并传递默认参数

    app/models/user.rb

      handle_asynchronously :send_reset_password_instructions
      handle_asynchronously :send_confirmation_instructions
      handle_asynchronously :send_on_create_confirmation_instructions 
    

    使用延迟作业活动记录 gem 发送带有排队过程的设计电子邮件

    #创建新用户后发送确认邮件

    def send_on_create_confirmation_instructions
        Devise::Mailer.delay.confirmation_instructions(self, :confirmation_instructions, opts={})
      end
    

    #发送重置密码邮件

    def send_reset_password_instructions
        Devise::Mailer.delay.reset_password_instructions(self, :reset_password_instructions, opts={})
      end
    

    #send 解锁电子邮件

    def send_unlock_instructions
        Devise::Mailer.delay.unlock_instructions(self, :unlock_instructions, opts={})
      end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-03
      • 1970-01-01
      • 1970-01-01
      • 2015-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-30
      相关资源
      最近更新 更多