ActionMailer是否仅在对其显式类名调用方法时呈现视图? (即不是“自我”)

时间:2012-02-25 11:07:19

标签: ruby-on-rails ruby actionmailer

我注意到我的CustomMailer(ActionMailer的子类)上出现了非常奇怪的行为:

class CustomMailer < ActionMailer::Base

  def deliver(template) # with ":greeting" passed as argument

    # WORKS: Renders view app/views/custom_mailer/greeting
    CustomMailer.send(template)

    # BROKEN: Does not render view app/view/custom_mailer/greeting
    self.send(template)
  end

  def greeting # this gets called by both invocations, but
               # only the former renders the view. Why?
    ...
    mail(to: ...

为什么第一个渲染但不渲染第二个?以下是我的假设:

    在没有导致恶作剧的情况下,send无法调用
  • self
  • 允许ActionMailer呈现视图(几乎是模型)的疯狂魔法依赖于明确调用CustomMailer的目标方法
  • [更新]关于self和实例方法与CustomMailer和类方法的内容?

1 个答案:

答案 0 :(得分:0)

ActionMailer :: Base overloads the class' method_missing而是创建一个实例,最终调用实例方法和/或呈现视图。