João Souza
João Souza

Reputation: 4208

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:

class ProductMailer < ApplicationMailer
  def sample_email
    mail(to: "[email protected]") # I hardcode my own email just to test
  end
end

And a very simple call from 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. The Sidekiq Web UI shows failed jobs in the Tries page and I can see why it failed:

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. 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 notifybut maybe that's because I'm overwriting some method from ActionMailer base class, I don't know.

Upvotes: 0

Views: 1037

Answers (2)

Jo&#227;o Souza
Jo&#227;o Souza

Reputation: 4208

Answer: Restart 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.

I renamed the method because I am used to development environment, where you can change almost anything on the fly...

Upvotes: 2

Max Williams
Max Williams

Reputation: 32933

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
    ....

Upvotes: -2

Related Questions