简体   繁体   English

ActionMailer — NoMethodError:MyMailer:Class的未定义方法X

[英]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.

相关问题 Rails 5:NoMethodError:MyMailer的未定义方法“ helper” - Rails 5: NoMethodError: undefined method `helper' for MyMailer ActionMailer :: Base:Class的未定义方法(NoMethodError) - undefined method for ActionMailer::Base:Class (NoMethodError) #的未定义方法`override_actionmailer_config =&#39; <Spree::AppConfiguration:0x000000092faba8> (NoMethodError) - undefined method `override_actionmailer_config=' for #<Spree::AppConfiguration:0x000000092faba8> (NoMethodError) ActionMailer:my_class#create为nil:NilClass创建未定义的方法“ each”的NoMethodError - ActionMailer : NoMethodError in my_class#create undefined method `each' for nil:NilClass 使用ApplicationHelper的ActionMailer类中的未定义方法 - Undefined method in ActionMailer class with ApplicationHelper NoMethodError:nil的未定义方法&#39;env&#39;:ActionMailer :: TestCase中的NilClass - NoMethodError: undefined method 'env' for nil:NilClass in ActionMailer::TestCase NoMethodError:#的未定义方法“类型” <Class> - NoMethodError: undefined method `type' for #<Class> Rails 4中ActionMailer :: Base:Class的未定义方法&#39;async =&#39; - undefined method `async=' for ActionMailer::Base:Class in rails 4 NoMethodError(未定义方法),但是定义了类方法 - NoMethodError (undefined method) however class method is defined 来自类中Private方法的NoMethodError(未定义方法) - NoMethodError (undefined method) from Private method in class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM