【问题标题】:in rails set config.action_mailer.default_url_options to whatever the current host of the app is?在 Rails 中将 config.action_mailer.default_url_options 设置为应用程序的当前主机是什么?
【发布时间】:2012-03-07 23:40:24
【问题描述】:

如何在 config/environments/production.rb 中动态设置主机:

config.action_mailer.default_url_options = { :host => THE_HOST}

这是为了让应用在暂存和生产中都能正常工作。我们的登台服务器是 stage.app.com,链接需要到那里。

【问题讨论】:

    标签: ruby-on-rails host


    【解决方案1】:

    config/environments/production.rb 中这样做:

    config.action_mailer.default_url_options = { :host => 'app.com' }
    

    config/environments/staging.rb 中这样做:

    config.action_mailer.default_url_options = { :host => 'stage.app.com' }
    

    正如@BrettBender 评论的那样:

    您不需要动态设置主机。对于生产中的应用程序,将评估 production.rb。对于在暂存环境中运行的应用程序,rails 将自动加载暂存文件(与开发或您定义的任何自定义环境相同)

    【讨论】:

    • 首先,一个错字:当你的意思是config/environments/production.rb 时,你已经输入了config/production.rb,在staging 时也是如此。要扩展此答案,您无需动态设置主机。对于生产中的应用程序,将评估 production.rb。对于在暂存环境中运行的应用程序,rails 将自动加载暂存文件(与开发或您定义的任何自定义环境相同)。
    • 感谢@BrettBender。我将您所说的内容纳入我的回答中。
    • 如果我有两个站点在生产模式下运行怎么办?一个是实际生产,另一个 - 用于 QA。
    • @Paul 然后这样做:config.action_mailer.default_url_options = { :host => ENV['MAIL_HOST'] } 并在每个服务器上设置 MAIL_HOST 环境变量
    • 如何在暂存环境中运行应用程序?
    【解决方案2】:

    将此添加到 application_controller 中的前置过滤器:

    ActionMailer::Base.default_url_options = {:host => request.host_with_port}
    

    【讨论】:

    • 这绝对是一个不好的做法。您将发送电子邮件与请求绑定在一起,因此从基于非请求的调用发送的任何电子邮件都将失败:rake 任务、resque 工作人员等。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-05
    • 1970-01-01
    • 2015-10-19
    • 2010-10-19
    • 1970-01-01
    • 2014-08-12
    相关资源
    最近更新 更多