【问题标题】:How to share methods between ActionMailers如何在 ActionMailers 之间共享方法
【发布时间】:2011-12-25 23:33:06
【问题描述】:

我有两个邮件:

class Mailer1 < ActionMailer::Base
  def mail
    if check_something?
    end
  end

  private

  def check_something?
  end
end


class Mailer2 < ActionMailer::Base
  def another_mail
    if check_something?
    end
  end

  private

  def check_something?
  end
end

(我知道我可以为实际的邮件模板提取视图助手,但我怎样才能使它适用于控制器类型的“助手”方法 - 因为 ActionMailers 现在从抽象控制器派生。)

那么,我在哪里可以声明 check_something?,以及如何让我的两个邮寄者都可以访问它?

【问题讨论】:

    标签: ruby-on-rails-3.1 actionmailer helpers


    【解决方案1】:

    只需创建一个基类,就像默认情况下为您的 http 控制器使用 ApplicationController 一样:

    class AppMailer < ActionMailer::Base
      protected
      def check_something?
      end
    end
    
    class Mailer1 < AppMailer
      def mail
        if check_something?
        end
      end
    end
    
    
    class Mailer2 < AppMailer
      def another_mail
        if check_something?
        end
      end
    end
    

    【讨论】:

    • 我也看不到任何其他方式。这样就可以了。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-25
    • 2020-09-23
    • 1970-01-01
    • 2021-10-15
    • 2016-05-04
    • 2019-09-26
    相关资源
    最近更新 更多