【问题标题】:ActionView::Template::Error: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to trueActionView::Template::Error: 缺少要链接的主机!请提供 :host 参数,设置 default_url_options[:host],或设置 :only_path 为 true
【发布时间】:2014-04-12 21:27:38
【问题描述】:

我的 ruby​​ on rails action mailer 在开发环境中运行良好,但在生产环境中,它一直抛出:

ActionView::Template::Error: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true

我的开发配置是

config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
  :port           => xxx,
  :address        => 'smtp.example.org',
  :user_name      => 'xxx@example.com',
  :password       => 'xxxxxxxx',
  :domain         => 'xxxxxx.com',
  :authentication => :plain,
}

config.action_mailer.default_url_options = { :host => 'localhost:3000' }

我的生产配置是

config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
  :port           => 587,
  :address        => 'smtp.example.org',
  :user_name      => 'xxx@example.com',
  :password       => 'xxxxxx',
  :domain         => 'example.com',
  :authentication => :plain,
}
config.action_mailer.default_url_options = { :host => 'example.com' }

我的其他环境是:

ruby 2.1.1
rails 4.0.3
sidekiq 
devise
devise-async

我试过了:

  1. 在初始化文件中添加以下内容

    ActionMailer::Base.default_url_options[:host] = "example.com"
    
  2. 这个答案here

它们都不起作用。

【问题讨论】:

  • 这很奇怪。我刚刚遇到了同样的错误,将 config.action_mailer.default_url_options = { :host => "example.com" } 添加到我的 application.rb 解决了这个问题......你确定你的配置被正确选择了吗?

标签: ruby-on-rails-4 devise actionmailer production


【解决方案1】:

最后我在每个环境文件中添加了正确的值:

test.rb / development.rb / production.rb

设计 3.2.x - 4.x.x

Rails 4.1.x && Rails 4.2.x && Rails 5.x.x

感谢maudulus

# Default Mailer Host
  Rails.application.routes.default_url_options[:host] = 'domain.com'

【讨论】:

  • 感谢您的解决方案。我能够从 ENV 变量加载。导轨 4.1.7.
  • 终于找到了一个可行的解决方案,这个问题是一个完美的例子,Stackoverflow 上的大多数结果都已经过时或很糟糕。
  • 这对我来说是 Rails 5.0.0 和 Devise 4.2.0 的正确答案
  • 谢谢!在我的情况下唯一有效的解决方案(Rails 5.0)!
  • 最后,一个帮助我的解决方案没有设置 config.default_url_options = { :host => 'localhost' }
【解决方案2】:

尝试重新启动服务器。即使在 Rails 4.1 中,配置也不会在不重新启动的情况下重新解析。

【讨论】:

  • 除此之外,如果您要从控制台触发邮件操作,您可能还需要重新启动控制台。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-12-31
  • 2019-12-11
  • 2011-05-06
  • 1970-01-01
  • 2016-07-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多