ActionView::Template::Error: 缺少链接的主机!请提供:host参数,设置default_url_options[:host],或将:only_path设置为true。

28

我在开发环境下的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.example',
  :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"
  • 这个答案在这里

  • 两者都不起作用。


    这很奇怪。我刚刚遇到了同样的错误,将config.action_mailer.default_url_options = { :host => "http://example.com" }添加到我的application.rb中解决了问题...你确定你的配置被正确地读取了吗? - valo
    2个回答

    84

    最终,我在每个环境文件中添加了以下内容,确保每个值都是正确的:

    test.rb / development.rb / production.rb

    Devise 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.example'
    

    3
    谢谢您提供的解决方案。我已经成功地从环境变量中加载了数据。这是在Rails 4.1.7版本下完成的。 - Mark Schneider
    1
    终于有一个可行的解决方案了,这个问题是一个完美的例子,大多数在Stackoverflow上的结果都已经过时或者只是糟糕的。 - b1nary
    2
    这对于我来说是Rails 5.0.0和Devise 4.2.0的正确答案。 - ramijames
    1
    谢谢!这是我(Rails 5.0)唯一有效的解决方案! - anthony
    1
    最终,有一个解决方案帮助了我,而不是设置config.default_url_options = { :host => 'localhost' }。 - asalgan
    显示剩余2条评论

    4
    尝试重新启动服务器。即使在Rails 4.1中,没有重新启动,配置也不会被重新解析。

    此外,如果您正在从控制台触发邮件操作,您可能也需要重新启动控制台。 - Mike B

    网页内容由stack overflow 提供, 点击上面的
    可以查看英文原文,
    原文链接