在 Rails 中是否可以通过模型中的属性在 Action Mailer 中设置默认值,而不必每次都在操作中设置它?
我有一个 Multi-Tenancy 应用程序,不同的租户有不同的电子邮件,这必须是邮件程序中的默认值。
根据文档,
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
最佳答案
刚刚发现这行得通。
class ApplicationMailer < ActionMailer::Base
after_action :set_default_from
private
def set_default_from
mail.to = tenant_email
end
关于ruby-on-rails - Action Mailer Rails 的动态默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52846887/