質問

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

役に立ちましたか?

解決

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.

他のヒント

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top