缺少主机链接!请提供:host参数或设置default_url_options [:host]

时间:2011-08-28 08:02:16

标签: ruby-on-rails rspec

我一直在谷歌上搜索大约90分钟,仍然没有答案。我在哪里设置default_url_options?我已经为config.action_mailer.default_url_options设置了它来解决其他地方的相同错误,但现在我在尝试在RSpec规范中使用URL帮助时遇到此错误。我不知道它在哪里设置default_url_options。

 Failure/Error: listing_url(listing).should match(/\/\d+-\w+$/)
 RuntimeError:
   Missing host to link to! Please provide :host parameter or set default_url_options[:host]
 # ./spec/routing/listing_routing_spec.rb:9:in `block (3 levels) in <top (required)>'

此代码与电子邮件/ ActionMailer无关,它恰好需要一个URL而不是路径。

有什么想法吗?

15 个答案:

答案 0 :(得分:236)

您需要在每个环境中添加以下行:

<强> config.action_mailer.default_url_options = { :host => "yourhost" }

这样,它可以在所有环境中工作,并且可能因环境而异。例如:

<强> development.rb

config.action_mailer.default_url_options = { :host => "dev.yourhost.com" }

<强> test.rb

config.action_mailer.default_url_options = { :host => "test.yourhost.com" }

<强> production.rb

config.action_mailer.default_url_options = { :host => "www.yourhost.com" }

答案 1 :(得分:62)

Your::Application.routes.draw do
  default_url_options :host => "example.com"

  # ... snip ...
end

routes.rb中的某处:)

答案 2 :(得分:38)

应在每个环境的配置文件中指定主机。例如:

config/environments/development.rb

请参阅this questionthis question

答案 3 :(得分:24)

当您使用任何listing_url方法时,将返回完整的URL(而不是正常的相对URL)。这就是为什么rails要求您提供主机,以计算整个URL。

如何告诉主持人?您可以通过以下几种方式实现:

1.将此选项添加到每个环境:

[/config/development.rb]
config.action_mailer.default_url_options = { host: "localhost:3000" }
[/config/test.rb]
config.action_mailer.default_url_options = { host: "localhost:3000" }
[/config/production.rb]
config.action_mailer.default_url_options = { host: "www.example.com" }

注意:如果您在 rails引擎内部工作,请记住在引擎测试中为您的虚拟应用做同样的事情:path_to_your_engine/test/dummy/config/environments/*因为当您测试时这是引擎正在测试的引擎。

2.将主机选项添加到foo_url方法,如下所示:

listing_url(listing, host: request.host) # => 'http://localhost:3000/listings/1'

3.不使用选项:only_path to true 输出主机。

listing_url(listing, only_path: true ) # => '/listings/1'   

恕我直言,我没有看到这一点,因为在这种情况下我会使用listing_path方法

答案 4 :(得分:22)

设置default_url_options以使用您的action_mailer.default_url_options

在每个环境文件中(例如development.rbproduction.rb等),您可以指定用于default_url_options的{​​{1}}:

action_mailer

但是,这些内容未设置为config.action_mailer.default_url_options = { host: 'lvh.me', port: '3000' }

MyApp:Application.default_url_options

这就是为什么你在$ MyApp::Application.config.action_mailer.default_url_options #=> {:host=>"lvh.me", :port=>"3000"} $ MyApp::Application.default_url_options #=> {} 以外的任何地方收到错误的原因。

您可以将应用程序ActionMailer设置为在相应的环境文件(default_url_optionsaction_mailer等)中使用您为development.rb定义的内容。

要尽可能保持干净,请在production.rb文件中执行此操作,这样您只需执行以下操作:

config/environment.rb

现在,当您启动应用时,您的整个应用# Initialize the rails application MyApp::Application.initialize! # Set the default host and port to be the same as Action Mailer. MyApp::Application.default_url_options = MyApp::Application.config.action_mailer.default_url_options 将与您的default_url_options匹配:

action_mailer.default_url_options

@pduersteler提示,让我沿着这条路走下去。

答案 5 :(得分:14)

有趣的是,设置config.action_mailer.default_url_options对我没有帮助。此外,在我感觉不属于的地方搞乱与环境无关的设置对我来说并不令人满意。另外,我想要一个在sidekiq / resque worker中生成URL的解决方案。

我的方法到目前为止,进入config/environments/{development, production}.rb

MyApp::Application.configure do
    # Stuff omitted...

    config.action_mailer.default_url_options = {
      # Set things here as usual
    }
end

MyApp::Application.default_url_options = MyApp::Application.config.action_mailer.default_url_options

这适用于rails&gt; = 3.2.x.

答案 6 :(得分:6)

您始终可以将主机作为参数传递给URL帮助程序:

listing_url(listing, host: request.host)

答案 7 :(得分:3)

您可以在Application Controller中设置默认网址选项:

class ApplicationController < ActionController::Base
  def default_url_options
    {:locale => I18n.locale}
  end
end

http://guides.rubyonrails.org/action_controller_overview.html#default_url_options

答案 8 :(得分:3)

Rails.application.routes.default_url_options[:host]= 'localhost:3000'

在developemnt.rb / test.rb中,可以更加简洁如下:

Rails.application.configure do
  # ... other config ...

  routes.default_url_options[:host] = 'localhost:3000'
end

答案 9 :(得分:2)

以防万一有人发现此搜索有关ActiveStorage的错误:

如果您有一个控制器操作,您想使用本地光盘服务生成上载网址等(最有可能在测试环境中),则需要在控制器中include ActiveStorage::SetCurrent才能允许{{ 1}}正常工作。

答案 10 :(得分:1)

我有同样的错误。我已经正确编写了所有内容,包括教程中的清单10.13。

Rails.application.configure do
.
.
.
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delevery_method :test
host = 'example.com'
config.action_mailer.default_url_options = { host: host }
.
.
.
end

显然,“example.com”已替换为我的服务器网址。

我在教程中掩饰的是这一行:

  

重新启动开发服务器以激活配置...

所以我的答案是关闭服务器并重新打开。

答案 11 :(得分:0)

在路由中添加default_url不是正确的解决方案,但它适用于某些情况。

您可以在每个环境(开发,测试,制作)中设置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/development.rb
     config.action_mailer.default_url_options = 
      { :host => 'your-host-name' }  #if it is local then 'localhost:3000'

答案 12 :(得分:0)

转到config / environments / test.rb

Rails.application.routes.default_url_options [:host] ='localhost:3000'

答案 13 :(得分:0)

我通过将environment.rb配置为

解决了该问题

YourApp :: Application.default_url_options = YourApp :: Application.config.action_mailer.default_url_options

您需要针对每种环境(例如开发,测试,登台和生产等)为动作邮件程序设置default_url_options。

参考:Missing host to link to! Please provide :host parameter or set default_url_options[:host]

答案 14 :(得分:0)

以上答案对我不起作用,至少不是我想要的那样。 我意识到 config.action_mailer.default_url_options = { host: "localhost", port: 3000 } 安装devise之后。 希望它能帮助遇到同样问题的人。

相关问题