João Marcelo Oliveira February 2016

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: "me@example.com") # 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.

Answers


Max Williams February 2016

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


João Marcelo Oliveira February 2016

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

Post Status

Asked in February 2016
Viewed 3,873 times
Voted 11
Answered 2 times

Search




Leave an answer