没有主机链接到! 请提供:host参数或设置default_url_options

我现在一直在search90分钟左右,但仍然没有答案。 我在哪里设置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而不是一个path。

有任何想法吗?

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

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" } 

我似乎今天回答我所有的问题!

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

某处在routes.rb 🙂

主机应该在每个环境的configuration文件中指定。 例如:config / environments / development.rb。

看到这个问题和这个问题 。

当你使用任何的listing_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引擎,请记住在引擎testing中为您的虚拟应用程序执行相同的操作: path_to_your_engine/test/dummy/config/environments/*因为当您testing引擎时,这就是Rails正在testing的内容。

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

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

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

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

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

有趣的是,设置config.action_mailer.default_url_options不帮助我。 而且,在我觉得不属于自己的地方搞乱环境独立的设置对我来说并不令人满意。 另外,我想要一个在sidekiq / resque工作中产生url的解决scheme。

到目前为止,我的方法进入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> = 3.2.x.

您始终可以将主机作为parameter passing给URL助手:

 listing_url(listing, host: request.host) 

您可以在应用程序控制器中设置默认的URL选项:

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

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

我有这个相同的错误。 我已经正确写入了一切,包括教程中的清单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”取代我的服务器的url。

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

重新启动开发服务器以激活configuration…

所以我的答案是closures服务器,然后再打开。

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

你必须在每个环境(开发,testing,生产)中设置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'