【发布时间】:2011-06-08 23:21:27
【问题描述】:
我刚刚开始考虑使用 delay_job gem。
为了测试它,我在欢迎电子邮件功能中添加了“延迟”并将该调用更改为
UserMailer.welcome_email(self).deliver
到
UserMailer.delay.welcome_email(self)
这在用户模型 after_create 中被调用。函数执行后,我看到延迟作业表中出现了一个条目。现在,当我在命令行上运行“rake jobs:work”时,任务会启动,但会出现如下错误
[Worker(host:Sanjay-PC pid:7008)] Starting job worker
[Worker(host:Sanjay-PC pid:7008)] Class#welcome_email failed with NoMethodError: undefined method `welcome_email' for #<Class:0x4871d60> - 0 failed attempts
[Worker(host:Sanjay-PC pid:7008)] 1 jobs processed at 0.0939 j/s, 1 failed ...
认为如果我将welcome_email方法声明更改为Class方法
def self.welcome_email(user)
(在前面添加 self.)这可能会有所帮助。但是当我运行 rake jobs:work 时出现以下错误
rake aborted!
undefined method `welcome_email' for class `UserMailer'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.5/lib/active_support/core_ext/module/aliasing.rb:31:in `alias_method'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.5/lib/active_support/core_ext/module/aliasing.rb:31:in `alias_method_chain'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/delayed_job-2.1.4/lib/delayed/message_sending.rb:50:in `handle_asynchronously'
c:/mgn/mgn-r3/app/mailers/user_mailer.rb:10:in `<class:UserMailer>'
c:/mgn/mgn-r3/app/mailers/user_mailer.rb:1:in `<top (required)>'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:454:in `load'
<Stack truncated>
它现在似乎知道这个类是 UserMailer,但不知何故它没有看到类方法welcome_email。
我在 Rails 3.0.5、Ruby 1.9.2p180 上,安装的延迟作业 gem 是 2.1.4 - 在 Windows 上
似乎在任何地方都找不到任何相关答案。
感谢您的意见。
-S
根据@pjammer 的请求添加 UserMailer 代码
class UserMailer < ActionMailer::Base
default :from => "from@example.com"
def welcome_email(user)
@user = user
@url = "http://example.com/login"
mail(:to => user.email,
:subject => "Welcome to My Awesome Site")
end
end
【问题讨论】:
-
请为您的邮件类添加代码,
UserMailer.welcome_email(self).deliver工作了吗?
标签: ruby-on-rails ruby-on-rails-3 delayed-job