【问题标题】:How to set the ActionMailer default_url_options's :host dynamically to the request's hostname?如何将 ActionMailer default_url_options 的 :host 动态设置为请求的主机名?
【发布时间】:2011-03-26 20:27:19
【问题描述】:

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

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

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

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

当我尝试通过

设置它时
config.action_mailer.default_url_options = {
  :host => request.domain
}

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

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

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

【问题讨论】:

    标签: ruby-on-rails ruby


    【解决方案1】:

    还可以通过在 default_url_options 哈希中设置 :host 选项来设置将在所有邮件中使用的默认主机

    application_controller.rb 中添加:

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

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

    或者,您可以在从控制器调用邮件功能时传递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
    

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

    【讨论】:

    • ActionMailer::Base 无权访问请求对象。
    • 过滤前的方法有效,但不是线程安全的,只适用于涉及单个域的情况。
    • 过滤方法之前 - 每个环境再见'config.action_mailer.default_url_options'! :)
    • @BradWerth 你能否解释一下为什么这只适用于使用单个域的应用程序?
    • @pixelearth 有点像stackoverflow.com/questions/38587612/…,但对于主机
    【解决方案2】:

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

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

    # 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
    

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

    【讨论】:

    • 显然(并且令人愤怒地)这不再被允许了。我收到此错误:RuntimeError (You can no longer call ActionMailer::Base.default_url_options directly. You need to set config.action_mailer.default_url_options. If you are using ActionMailer standalone, you need to include the routing url_helpers directly.)
    • 不确定@nathan.f77。我可以设置它:ActionMailer::Base.default_url_options = {:host =&gt; request.host_with_port}
    • @SooDesuNe 现在已被弃用。不能再直接设置了。
    • 那么解决办法是什么?在我看来,虽然能够通过配置对象在初始化程序中设置内容很有用,但在初始化 ActionMailer 后应该总有一种方法可以修改配置......但这似乎仍然对我有用在 Rails 3.0.9 中,所以我想现在可以使用...
    • 使用这个线程安全的解决方案:stackoverflow.com/questions/2660172/…
    【解决方案3】:

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

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

    请参阅http://api.rubyonrails.org/classes/ActionMailer/Base.html 生成 url 部分。

    【讨论】:

      猜你喜欢
      • 2019-12-11
      • 1970-01-01
      • 2017-12-31
      • 1970-01-01
      • 2011-05-06
      • 2014-04-12
      • 1970-01-01
      • 2010-10-22
      • 1970-01-01
      相关资源
      最近更新 更多