【问题标题】:Dynamic default from with Action Mailer Rails来自 Action Mailer Rails 的动态默认值
【发布时间】:2018-10-17 03:29:51
【问题描述】:

是否可以在 Rails 中通过模型中的属性在 Action Mailer 中设置默认值,而不必每次都在操作中设置?

我有一个多租户应用程序,不同的租户有不同的电子邮件,这必须是邮件程序中的默认电子邮件。

根据文档,

    default to:       -> { 'user@email.test' },
    from:     -> { method_that_retrieves_tenant_email }

应该可以,但是当我尝试时,它会抛出以下错误

    Failure/Error: from:     -> { method },
    ArgumentError:
    wrong number of arguments (given 1, expected 0)

我正在使用 Rails 5.2.0 和 ruby​​ 2.3.1p112。

其他细节: 这是我用过的

    class ApplicationMailer < ActionMailer::Base 
      default from: -> { tenant_email }
      .....

      private
      def tenant_email
        fetch_email
      end

【问题讨论】:

  • 您是否尝试过在 Mailer(而不是模型)中设置它,但在特定操作之外?如果它们都来自同一个电子邮件地址,您可以从您的应用程序特定的邮件程序或 ApplicationMailer 中设置它。
  • 是的。我已在 ApplicationMailer 中添加。请检查上述问题中的其他详细信息部分。

标签: ruby-on-rails ruby ruby-on-rails-5


【解决方案1】:

刚刚发现这行得通。

    class ApplicationMailer < ActionMailer::Base

      after_action :set_default_from

    private

    def set_default_from
      mail.to = tenant_email
    end

【讨论】:

    猜你喜欢
    • 2012-09-14
    • 1970-01-01
    • 1970-01-01
    • 2012-01-14
    • 2012-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多