如何在 ActionMailers 之间共享方法

发布于 2024-12-14 09:48:24 字数 468 浏览 1 评论 0原文

我有两个邮件程序:(

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? ,如何才能让我的两个邮件程序都可以访问它?

I have two mailers:

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

(I understand I can pull in view helpers for the actual mail templates, but how can I make it work for controller-type "helper" methods - as ActionMailers derive from Abstract Controller these days.)

So, where can I declare check_something?, and how can I make it accessible to both my mailers?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哆兒滾 2024-12-21 09:48:24

只需创建一个基类,就像您的 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

Just create a base class, like you get by default with ApplicationController for your http controllers:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文