如何为每封邮件设置default_url_options?

时间:2017-04-27 08:42:03

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

在Rails 5中,由于某些商业原因,我们有两个邮件。

我们称他们为FooMailerBarMailer

业务要求是分别为每个人设置default_url_options

  • 对于FooMailer,他们应该是{ host: "wut.example.com" }
  • 对于BarMailer,他们应该是{ host: "blah.example.com" }

我该怎么做?

1 个答案:

答案 0 :(得分:2)

对于全球所有邮寄者:

config/environment/production.rb

config.action_mailer.default_url_options = { :host => 'http://abc.co.uk' }

每封邮件:

对于Rails 5.0.x,在每个邮件程序上设置default_url_options as方法似乎正在运行:

class FooMailer < ApplicationMailer
  ...
  def default_url_options
    { host: "wut.example.com" }
  end
  ...
end

class BarMailer < ApplicationMailer
  ...
  def default_url_options
    { host: "blah.example.com" }
  end
  ...
end

提示:如果您仍希望在特定于环境的文件中设置这些选项(就像使用全局默认设置一样),可以使用Rails.applicaiton.config.x

class FooMailer < ApplicationMailer
  ...
  def default_url_options
    Rails.application.config.x.default_foo_mailer_url_options ||
      raise('No x.default_foo_mailer_url_options config found')
  end
  ...
end

然后在config/environments/*.rb中你可以这样设置:

Rails.application.configure do
  ...
  config.x.default_foo_mailer_url_options = { 
    host: "wut.example.com" 
  }
  ...
end