新手问题 UserMailer 里边定义的方法都是类方法?

QETHAN · December 03, 2016 · Last by zengfengbo replied at January 02, 2017 · 1777 hits
class UserMailer < ApplicationMailer
  default from: "[email protected]"


  def account_activation(user)
    @user = user
    mail to: user.email, subject: "账户激活"
  end

  def password_reset
    @greeting = "Hi"

    mail to: "[email protected]"
  end
end

在控制器里使用的时候都是直接用 UserMailer.account_activation(user), 不明白 account_activation 为什么变成了类方法

父类 ActionMailer::Base 有个 method_missing 方法
父类的父类 AbstractController::Base 有个 action_methods 方法

直接调用类方法 account_activation 会触发 method_missing
经过 method_missingaction_methods 进行处理 & 调用对应的实例方法 account_activation

blacktulip in ApplicationMailer 奇怪的调用方法 mention this topic. 24 Dec 00:45

为什么要这么处理呢?直接定义类方法不好?

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