Ruby-on-rails 缺少要链接的主机!请提供 :host 参数或设置 default_url_options[:host]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7219732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Missing host to link to! Please provide :host parameter or set default_url_options[:host]
提问by d11wtq
I have been googling for about 90 minutes now and still don't have an answer to this. Where do I set default_url_options? I've already set it for config.action_mailer.default_url_optionsto solve this same bug elsewhere, but now I'm getting this error when trying to use a URL helper inside an RSpec spec. I have no idea where it's expecting default_url_options to be set.
我已经在谷歌上搜索了大约 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)>'
This code has nothing to do with emails/ActionMailer, it just happens to need a URL instead of a path.
这段代码与 emails/ActionMailer 无关,它只是需要一个 URL 而不是路径。
Any ideas?
有任何想法吗?
采纳答案by d11wtq
Your::Application.routes.draw do
default_url_options :host => "example.com"
# ... snip ...
end
Somewhere in routes.rb:)
某处routes.rb:)
回答by Carlos Castillo
You need to add the following line at every environment:
您需要在每个环境中添加以下行:
config.action_mailer.default_url_options = { :host => "yourhost" }
config.action_mailer.default_url_options = { :host => "yourhost" }
That way, it can work in all environments and could be different from environment to environment. For example:
这样,它可以在所有环境中工作,并且可能因环境而异。例如:
development.rb
发展.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
生产.rb
config.action_mailer.default_url_options = { :host => "www.yourhost.com" }
回答by nickh
The host should be specified in each environment's config file. Eg:
主机应在每个环境的配置文件中指定。例如:
config/environments/development.rb
See this questionand this question.
回答by Joshua Pinter
Set default_url_optionsto use your action_mailer.default_url_options.
设置default_url_options为使用您的action_mailer.default_url_options.
In each of your environment files (e.g. development.rb, production.rb, etc.) you can specify the default_url_optionsto use for action_mailer:
在您的每个环境文件(例如development.rb、production.rb等)中,您可以指定default_url_options要用于action_mailer:
config.action_mailer.default_url_options = { host: 'lvh.me', port: '3000' }
However, these are not set for MyApp:Application.default_url_options:
但是,这些未设置为MyApp:Application.default_url_options:
$ MyApp::Application.config.action_mailer.default_url_options
#=> {:host=>"lvh.me", :port=>"3000"}
$ MyApp::Application.default_url_options
#=> {}
That's why you are getting that error in anything outside of ActionMailer.
这就是为什么您在ActionMailer.
You can set your Application's default_url_optionsto use what you defined for action_mailerin the appropriate environment file (development.rb, production.rb, etc.).
您可以设置应用程序的default_url_options使用您定义的内容action_mailer在适当的环境文件(development.rb,production.rb,等)。
To keep things as DRY as possible, do this in your config/environment.rbfile so you only have to do this once:
为了尽可能保持干燥,请在您的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
Now when you boot up your app, your entire Application's default_url_optionswill match your action_mailer.default_url_options:
现在,当您启动您的应用程序时,您的整个应用程序default_url_options将匹配您的action_mailer.default_url_options:
$ MyApp::Application.config.action_mailer.default_url_options
#=> {:host=>"lvh.me", :port=>"3000"}
$ MyApp::Application.default_url_options
#=> {:host=>"lvh.me", :port=>"3000"}
Hat tip to @pduerstelerfor leading me down this path.
帽尖到@pduersteler领导我走上这条道路。
回答by ivanxuu
When you use any listing_urlmethod the full URL will be returned(not a relative one as normal). That's why rails is asking you for the host, to compute the whole URL.
当您使用任何listing_url方法时,将返回完整的 URL(不是正常的相对 URL)。这就是 Rails 要求您提供主机以计算整个 URL 的原因。
How you can tell rails the host? You can do it in several ways:
你怎么能告诉 Rails 主机?您可以通过多种方式做到这一点:
1.Adding this option to each environment:
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" }
NOTE:If you are working inside a rails engineremember to do the same for your dummy app inside the engine tests: path_to_your_engine/test/dummy/config/environments/*because when you test the engine it's what rails is testing against.
注意:如果您在rails 引擎中工作,请记住在引擎测试中对您的虚拟应用程序执行相同的操作:path_to_your_engine/test/dummy/config/environments/*因为当您测试引擎时,它就是 rails 测试的对象。
2.Add the host option to the foo_url method like this:
2.将host选项添加到foo_url方法中,如下所示:
listing_url(listing, host: request.host) # => 'http://localhost:3000/listings/1'
3.Not output the host with the option :only_path to true.
3.不输出带有选项的主机:only_path to true。
listing_url(listing, only_path: true ) # => '/listings/1'
IMHO I don't see the point on this one because in this case I would use the listing_pathmethod
恕我直言,我没有看到这一点,因为在这种情况下我会使用该listing_path方法
回答by pduersteler
Funny thing, that setting config.action_mailer.default_url_optionsdoes not help for me. Also, messing around with environment-independent settings in places I felt like it does not belong was not satisfying for me. Additionally, I wanted a solution that worked when generating urls in sidekiq/resque workers.
有趣的是,该设置config.action_mailer.default_url_options对我没有帮助。此外,在我觉得它不属于的地方乱搞与环境无关的设置对我来说并不令人满意。此外,我想要一个在 sidekiq/resque 工作器中生成 url 时有效的解决方案。
My approach so far, which goes into config/environments/{development, production}.rb:
到目前为止,我的方法是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
This works for me in rails >= 3.2.x.
这在 Rails >= 3.2.x 中对我有用。
回答by Derek Fan
Rails.application.routes.default_url_options[:host]= 'localhost:3000'
Rails.application.routes.default_url_options[:host]= 'localhost:3000'
In the developemnt.rb / test.rb, can be more concise as following:
在developemnt.rb/test.rb中,可以更简洁的如下:
Rails.application.configure do
# ... other config ...
routes.default_url_options[:host] = 'localhost:3000'
end
回答by Undistraction
You can always pass host as a parameter to the URL helper:
您始终可以将主机作为参数传递给 URL 助手:
listing_url(listing, host: request.host)
回答by RicRoberts
You can set default url options in the Application Controller:
您可以在应用程序控制器中设置默认 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
http://guides.rubyonrails.org/action_controller_overview.html#default_url_options
回答by milad rahmani
go to config/environments/test.rb
去配置/环境/test.rb
Rails.application.routes.default_url_options[:host] = 'localhost:3000'
Rails.application.routes.default_url_options[:host] = 'localhost:3000'

