Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

private method `new' called for MyReminderMailer:Class

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

like image 899
user1510412 Avatar asked Dec 02 '22 17:12

user1510412


2 Answers

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.

like image 120
Zach Kemp Avatar answered Dec 26 '22 00:12

Zach Kemp


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.

like image 34
Chen Avatar answered Dec 26 '22 01:12

Chen