[英]ActionMailer — NoMethodError: undefined method X for MyMailer:Class
Question: Why is the method undefined if it's just there? 问题:为什么该方法只是未定义的?
Details: 细节:
I have a very simple mailer class: 我有一个非常简单的mailer类:
class ProductMailer < ApplicationMailer
def sample_email
mail(to: "me@example.com") # I hardcode my own email just to test
end
end
And a very simple call from ProductsController: 来自ProductsController的一个非常简单的调用:
def sample_email
ProductMailer.sample_email().deliver_later
redirect_to @product, notice: 'Email was queued.'
end
The email fails to be sent. 电子邮件发送失败。 I am using Sidekiq to process emails in background.
我正在使用Sidekiq在后台处理电子邮件。 The Sidekiq Web UI shows failed jobs in the Tries page and I can see why it failed:
Sidekiq Web UI在“ 尝试”页面中显示失败的作业,我可以看到失败的原因:
NoMethodError: undefined method `sample_email' for ProductMailer:Class
I tried to rename the method and restart the server with rails server
but none of that removes the error. 我试图重命名该方法,并使用
rails server
重新启动服务器,但是这些都不能消除错误。 I am not using any namespaces. 我没有使用任何名称空间。
Question: Why is the method undefined if it's just there? 问题:为什么该方法只是未定义的?
Note: I found out by chance that the method is found if I name it notify
but maybe that's because I'm overwriting some method from ActionMailer base class, I don't know. 注:我发现一个偶然的机会,如果我命名的方法发现
notify
,但也许是因为我从覆盖的ActionMailer基类的一些方法,我不知道。
Answer: Restart Sidekiq 答:重新启动Sidekiq
I created the mailer class before starting Sidekiq, but I renamed the sample_email
method while Sidekiq was already running, so it seems that Sidekiq doesn't recognize new methods on-the-fly. 我在启动Sidekiq之前创建了mailer类,但是在Sidekiq已经运行时我将其重命名为
sample_email
方法,因此似乎Sidekiq不能即时识别新方法。
I renamed the method because I am used to development environment, where you can change almost anything on the fly... 我重命名该方法是因为我习惯了开发环境,在该环境中您可以随时更改几乎所有内容。
It's because you've defined an instance method, and then you try to call it on a class. 这是因为您已经定义了实例方法,然后尝试在类上调用它。 Change it to
更改为
def self.sample_email
....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.