我正在尝试延迟通知电子邮件,以便在注册到我的应用程序时发送给用户.电子邮件是使用我称之为InitMailer的ActionMailer发送的.我试图延迟工作的方式是使用collectidea的delayed_job
https://github.com/collectiveidea/delayed_job.为此,您可以看到我在定义方法initial_email之后指定handle_isynchronously:
class InitMailer < ActionMailer::Base default :from => "info@blahblahblah.com" def initial_email(user) @user = user @url = "http://www.blahblahblah.com" mail(:to => user.email,:subject => "Welcome to my website!" ) end handle_asynchronously :initial_email end
但是,我的日志文件“delayed_job.log”中遇到一个参数错误:
Class#initial_email Failed with ArgumentError: wrong number of arguments (1 for 0) - 5 Failed attempts
有关您的信息,电子邮件将在控制器中使用以下行:
@user = InitUser.new(params[:init_user]) InitMailer.delay.initial_email(@user)
解决方法
由于Rails3实现邮件程序的方式,对于delayed_jobs有一些不寻常的解决方法.例如,你已经看到要延迟邮寄,你写
ExampleMailer.delay.example(user)
虽然通常,您必须在方法定义之后写入handle_synchronously,在邮件程序的情况下,此声明(由于某种原因)会阻止该延迟作业工作.
所以在这段代码中,完全放弃声明:
class InitMailer < ActionMailer::Base default :from => "info@blahblahblah.com" def initial_email(user) @user = user @url = "http://www.blahblahblah.com" mail(:to => user.email,:subject => "Welcome to my website!" ) end #No handle_asynchronously needed here end