Ruby-on-rails 如何将 ActionMailer default_url_options 的 :host 动态设置为请求的主机名?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3432712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:00:40  来源:igfitidea点击:

How to set the ActionMailer default_url_options's :host dynamically to the request's hostname?

ruby-on-railsruby

提问by Madhusudhan

I am trying to set the :host for action mailer default url options.

我正在尝试设置 :host for action mailer 默认 url 选项。

I have the below set in all the environment files

我在所有环境文件中都设置了以下内容

config.action_mailer.default_url_options = {
  :host => "localhost"
}

I want to make it more dynamic by providing the request host.

我想通过提供请求主机使其更加动态。

when I try to set it by

当我尝试设置它时

config.action_mailer.default_url_options = {
  :host => request.domain
}

OR

或者

config.action_mailer.default_url_options = {
  :host => request.env["SERVER_NAME"]
}

It throws error... doesn't recognize "request" object

它抛出错误...无法识别“请求”对象

is there any way I can set this to the request host, not by hardcoding...?

有什么方法可以将其设置为请求主机,而不是通过硬编码...?

回答by montrealmike

It is also possible to set a default host that will be used in all mailers by setting the :host option in the default_url_options hash

也可以通过在 default_url_options 散列中设置 :host 选项来设置将在所有邮件程序中使用的默认主机

in an application_controller.rbadd:

在一个application_controller.rb补充:

class ApplicationController < ActionController::Base
  def default_url_options
    { host: request.host_with_port }
  end
end

Source: https://edgeguides.rubyonrails.org/action_controller_overview.html#default-url-options

来源:https: //edgeguides.rubyonrails.org/action_controller_overview.html#default-url-options

Alternatively, you can pass the requestwhen calling the mailer function from the controller

或者,您可以在request从控制器调用邮件程序函数时传递

class UserMailer < ActionMailer::Base

  def welcome_email(user, request)
    @user = user
    @url  = user_url(@user, host: request.host_with_port ) # do this for each link
    mail(:to => user.email, :subject => "Welcome to My Awesome Site")
  end
end

Source : https://guides.rubyonrails.org/action_mailer_basics.html#generating-urls-with-named-routes

来源:https: //guides.rubyonrails.org/action_mailer_basics.html#generating-urls-with-named-routes

回答by Diego Plentz

UPDATE: use the selected response, since this isn't thread safe.

更新:使用选定的响应,因为这不是线程安全的。

You can create a default filter like this:

您可以像这样创建一个默认过滤器:

# application_controller.rb
before_filter :mailer_set_url_options

...

def mailer_set_url_options
  ActionMailer::Base.default_url_options[:host] = request.host_with_port
end

(source: http://www.cherpec.com/2009/06/missing-host-to-link-to-please-provide-host-parameter-or-set-default_url_optionshost/)

(来源:http: //www.cherpec.com/2009/06/missing-host-to-link-to-please-provide-host-parameter-or-set-default_url_optionshost/

回答by Doon

the problem is these are initializers, they are run when the rails stack loads, and not when you call active mailer.

问题是这些是初始化程序,它们在 Rails 堆栈加载时运行,而不是在您调用活动邮件程序时运行。

but you don't have to use the default_url, you can just pass the hostname into the url_for/named routes in each of your mailer views. The default just avoids having to do that.

但是您不必使用 default_url,您可以将主机名传递到每个邮件程序视图中的 url_for/named 路由中。默认设置只是避免了这样做。

see http://api.rubyonrails.org/classes/ActionMailer/Base.htmlsection on generating urls.

参见http://api.rubyonrails.org/classes/ActionMailer/Base.html关于生成 url 的部分。