Rails actionmailer 中定义的是实例方法,调用是类方法,为什么?

chenge · January 21, 2013 · Last by chenge replied at January 23, 2013 · 2403 hits
class MyMailer <ActionMailer::Base
  def confirm(email)
        @message = "Thank you for confirmation!"
        mail(:to => email, :subject => "Registered")  
  end  
end

调用: MyMailer.confirm('[email protected]').deliver

定义的是实例方法,调用是类方法,为什么?

是因为用到了 Ruby 的 method_missing 方法,对 ActionMailer::Base 这个对象 (注意不是实例) 添加了 method_missing 方法,然后做了初始化一个实例 Mailer , 寻找实例方法,render 等等...

这些需要弄懂 ruby 的元编程再来看可能会好一点.

#1 楼 @wppurking 谢谢你的回答。只是觉得是没必要的绕弯子设计。

You need to Sign in before reply, if you don't have an account, please Sign up first.