我在Rails应用中的AnswerObserver < ActiveRecord::Observer
中有以下代码。创建答案后,会向发送问题的用户发送电子邮件。
def after_create(answer)
answer.question.watchers.each do |user|
Notifier.answer_updated(answer, user).deliver
end
Notifier继承自ActionMailer class Notifier < ActionMailer::Base
并具有方法
def answer_updated(answer, user)
@answer = answer
@user = user
mail(:to => user.email,
:subject => "Your question has been answered")
end
我现在正在尝试使用SideKiq将其移至后台进程。我安装并运行了redis,并使用bundle exec sidekiq
成功启动了sidekiq。
根据此博文http://blog.remarkablelabs.com/2013/01/using-sidekiq-to-send-emails-asynchronously,SideKiq内置了对ActionMailer的支持
The method that you will likely use the most is .delay. Calling .delay from an Action Mailer will result in the email being added to the DelayedMailer queue for processing.
因此,我在调用动作邮件程序方法answer_updated的观察者中用'delay'替换了“deliver”方法,就像这样
def after_create(answer)
answer.question.watchers.each do |user|
Notifier.answer_updated(answer, user).delay
end
但现在我收到了这个错误
undefined method `delay' for #<Mail::Message:0x007fee0c9af1d0>
我没有做任何其他事情来配置SideKiq。你能解释一下我可能做错了什么吗?
答案 0 :(得分:6)
答案 1 :(得分:0)
对我来说没有用。 delay
没有mailer
方法。看看这里:
>> RUBY_VERSION
=> "2.2.0"
>> Rails::VERSION::STRING
=> "4.2.1"
>> mailer.methods.include?(:delay)
=> false
>> mailer.send_email(mailable, recipient).methods.include?(:deliver_later)
=> true
所以,如果你使用Rails&gt; 4.2使用此:
Notifier.answer_updated(answer, user).deliver_later