ActionView::Template::Error: 缺少链接的主机名

25

正如标题所示,当我尝试在我的邮件模板中使用link_to时,我遇到了这个错误:

即使标题意味着,当我在邮件模板中使用 link_to 时,我会遇到此错误:

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

我尝试按照它的说明进行操作,并且我还找到了这篇文章,但是我仍然遇到了错误。

我已经尝试在以下每个文件中添加config.action_mailer.default_url_options = { host: 'example.com' },但是没有一个文件起作用:

/config/environment.rb
/config/application.rb (inside the module for my app)
/config/development.rb (I'm in development mode)

在Rails 4中是否有任何变化导致其工作方式不同?

2个回答

39

前几天我遇到了这个问题,以下是解决方法:

Rails.application.routes.default_url_options[:host] = '???'

编辑

这个内容需要放在每个环境文件中 - development.rb, test.rbproduction.rb(如果有其他的文件也需要加入),并在每个文件中使用相应的主机名。


你在哪个文件里添加了它? - emersonthis
2
config/application.rb是加载每种环境(dev、prod、test等)的配置文件。其他文件,如config/development.rb,则与相应的环境一起加载。所以这取决于您@Emerson,您可以根据自己的需求/环境进行选择。 - MrYoshiji
3
谢谢你。这个方法有效。我想了解更多关于这是什么以及为什么它有效的信息。这个方法有文件记录吗? - emersonthis
@MrYoshiji 如果config/application.rb是全局环境设置,那么config/environment.rb是什么? <--新手问题 - emersonthis
我在这里找到了答案,但其他地方也有 - 不幸的是它并没有真正包括“为什么”。 - dax

15

在每个环境(开发、测试、生产)中都需要设置 default_url。

你需要进行这些更改。

    config/environments/development.rb
     config.action_mailer.default_url_options = 
      { :host => 'your-host-name' }  #if it is local then 'localhost:3000'

 config/environments/test.rb
      config.action_mailer.default_url_options = 
      { :host => 'your-host-name' }  #if it is local then 'localhost:3000'

  config/environments/production.rb
     config.action_mailer.default_url_options = 
      { :host => 'your-host-name' }  #if it is local then 'localhost:3000'

谢谢。我只在development.rb文件中设置了这个;没想到还要在test.rb里设置。Rails 5.0.2,Ruby 2.4。 - Greg

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