Question

In a controller, i have:

mailer = MyReminderMailer.new

the mailer looks like this:

class MyReminderMailer < ActionMailer::Base
  def change_email
    mail(
      from:     default_from,
      to:       default_to,
      subject: "..."
    )
  end

  def default_from
    return '...'
  end

  def default_to
    return '...'
  end
end

but got error: private method `new' called for MyReminderMailer:Class

Was it helpful?

Solution

ActionMailer::Base has a rather goofy and unintuitive API. Much like controllers, you never explicitly create instances of your mailers. Instead, you interact with them as classes. new is marked private in ActionMailer::Base, and method calls on the class are subsequently routed through method_missing to a new instance of itself. Like I said, unintuitive.

Have a look at the guides and api docs for more information on the correct usage of ActionMailer.

OTHER TIPS

Ruby does not allow to call private method in normal way. You can call it with send method

SomeClass.send :method_name

#in your case
MyReminderMailer.send :new

And you don't need ActionMailer object. To send mail just use the method as like class method.

MyReminderMailer.change_email.deliver

Hope this can help you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top