Rails:缺少要链接的主机!请提供 :host 参数或设置 default_url_options[:host]
我已经在谷歌上搜索了大约 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)>'
这段代码与 emails/ActionMailer 无关,它只是需要一个 URL 而不是路径。
有什么想法吗?
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_options
to 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.
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.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
您需要在每个环境中添加以下行:
config.action_mailer.default_url_options = { :host =>; "yourhost" }
这样,它就可以在所有环境中工作,并且可能因环境而异。例如:
开发.rb
测试.rb
生产.rb
You need to add the following line at every environment:
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
test.rb
production.rb
在
routes.rb
中的某个地方:)Somewhere in
routes.rb
:)应在每个环境的配置文件中指定主机。例如:
参见这个问题和这个问题。
The host should be specified in each environment's config file. Eg:
See this question and this question.
设置
default_url_options
以使用您的action_mailer.default_url_options
。在每个环境文件(例如
development.rb
、development.rb
等)中,您可以指定用于default_url_options
>action_mailer:但是,这些未针对
MyApp:Application.default_url_options
设置:这就是为什么您在
ActionMailer
之外的任何内容中都会收到该错误。您可以将应用程序的
default_url_options
设置为使用您在适当的环境文件(development.rb
、development.rb)中为
等)。action_mailer
定义的内容为了尽可能保持干燥,请在您的
config/environment.rb
文件中执行此操作,这样您只需执行一次:现在,当您启动应用程序时,整个应用程序的
default_url_options< /code> 将匹配您的
action_mailer.default_url_options
:向 @pduersteler 带领我走上这条路。
Set
default_url_options
to use youraction_mailer.default_url_options
.In each of your environment files (e.g.
development.rb
,production.rb
, etc.) you can specify thedefault_url_options
to use foraction_mailer
:However, these are not set for
MyApp:Application.default_url_options
:That's why you are getting that error in anything outside of
ActionMailer
.You can set your Application's
default_url_options
to use what you defined foraction_mailer
in the appropriate environment file (development.rb
,production.rb
, etc.).To keep things as DRY as possible, do this in your
config/environment.rb
file so you only have to do this once:Now when you boot up your app, your entire Application's
default_url_options
will match youraction_mailer.default_url_options
:Hat tip to @pduersteler for leading me down this path.
当您使用任何
listing_url
方法时,将返回完整的 URL(而不是通常的相对 URL)。这就是为什么 Rails 要求您提供主机来计算整个 URL。你如何告诉rails主机?您可以通过多种方式完成此操作:
1.将此选项添加到每个环境:
注意:如果您在 rails 引擎中工作,请记住在引擎测试中对虚拟应用程序执行相同的操作:
path_to_your_engine/test/dummy/config/environments/*
因为当您测试引擎时,rails 正在测试引擎。2.将主机选项添加到 foo_url 方法,如下所示:
3.不使用选项
:only_path to true
输出主机。恕我直言,我没有看到这一点,因为在这种情况下我将使用
listing_path
方法When you use any
listing_url
method 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.How you can tell rails the host? You can do it in several ways:
1.Adding this option to each environment:
NOTE: If you are working inside a rails engine remember 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.2.Add the host option to the foo_url method like this:
3.Not output the host with the option
:only_path to true
.IMHO I don't see the point on this one because in this case I would use the
listing_path
methodRails.application.routes.default_url_options[:host]= 'localhost:3000'
在developemnt.rb / test.rb中,可以更简洁如下:
Rails.application.routes.default_url_options[:host]= 'localhost:3000'
In the developemnt.rb / test.rb, can be more concise as following:
有趣的是,设置 config.action_mailer.default_url_options 对我没有帮助。另外,在我觉得不属于的地方乱搞与环境无关的设置对我来说并不令人满意。此外,我想要一个在 sidekiq/resque 工作人员中生成 url 时有效的解决方案。
到目前为止,我的方法进入 config/environments/{development, production}.rb:
这对我来说在 Rails >= 3.2.x 中有效。
Funny thing, that setting
config.action_mailer.default_url_options
does 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.My approach so far, which goes into
config/environments/{development, production}.rb
:This works for me in rails >= 3.2.x.
您始终可以将主机作为参数传递给 URL 帮助器:
You can always pass host as a parameter to the URL helper:
上面的答案对我不起作用,至少不是我想要的。
我意识到
config.action_mailer.default_url_options = { 主机:“localhost”,端口:3000 }
安装设备后。
希望它能帮助遇到同样问题的人。
The above answer did not work for me, at least not as I wanted.
I realised
config.action_mailer.default_url_options = { host: "localhost", port: 3000 }
after installing devise.
Hope it will help someone with the same problem.
转到 config/environments/test.rb
Rails.application.routes.default_url_options[:host] = 'localhost:3000'
go to config/environments/test.rb
Rails.application.routes.default_url_options[:host] = 'localhost:3000'
以防万一有人发现此搜索有关 ActiveStorage 的错误:
如果您有一个控制器操作,想要使用本地光盘服务(最有可能在测试环境中)生成上传 URL 等,则需要包含 ActiveStorage ::SetCurrent 在控制器中,以便让
blob.service_url_for_direct_upload
正常工作。just in case someone finds this searching for errors concerning ActiveStorage:
if you have a controller-action where you want to generate upload-urls etc with the local disc-service (most likely in test environment), you need to
include ActiveStorage::SetCurrent
in the controller in order to allowblob.service_url_for_direct_upload
to work correctly.您可以在应用程序控制器中设置默认 url 选项:
http://guides.rubyonrails.org/action_controller_overview。 html#default_url_options
You can set default url options in the Application Controller:
http://guides.rubyonrails.org/action_controller_overview.html#default_url_options
我也有同样的错误。我已经正确编写了所有内容,包括教程中的清单 10.13。
显然用我的服务器网址替换了“example.com”。
我在教程中掩盖的是这一行:
所以我的答案是关闭服务器然后再次打开。
I had this same error. I had everything written in correctly, including the Listing 10.13 from the tutorial.
obviously with "example.com" replaced with my server url.
What I had glossed over in the tutorial was this line:
So the answer for me was to turn the server off and back on again.
在路由中添加 default_url 不是正确的解决方案,但它适用于某些情况。
您必须在每个环境(开发、测试、生产)中设置default_url。
你需要做出这些改变。
Adding the default_url in routes not the right solution although, it works for some cases.
You've to set the default_url in each environment(development, test, production).
You need make these changes.
解决了这个问题。
我通过将environment.rb配置为YourApp::Application.default_url_options = YourApp::Application.config.action_mailer.default_url_options
您需要针对每个环境(如开发、测试、登台和生产等)为操作邮件程序设置default_url_options。
参考: <一href="https://stackoverflow.com/questions/7219732/missing-host-to-link-to-please-provide-host-parameter-or-set-default-url-optio/48529627#48529627">缺少主机链接到!请提供 :host 参数或设置default_url_options[:host]
I solved the issue by configuring environment.rb as
YourApp::Application.default_url_options = YourApp::Application.config.action_mailer.default_url_options
You need to set default_url_options for action mailer against each environment like development, testing, staging and production etc.
Reference: Missing host to link to! Please provide :host parameter or set default_url_options[:host]
不想改变其他环境的行为,所以我使用:
development.rb
Works in Rails 6。
Didn't want to change the behavior for other environments, so I used:
development.rb
Works in Rails 6.
Rails.application.config.action_mailer.default_url_options
Rails.application.default_url_options
首先设置 Action Mailer 默认 url 选项(如大多数答案中所示)
,然后分配该选项到应用程序。最初的问题正是关于这个配置的!与邮件程序无关:)奇怪的是,问题很旧,但并非所有答案都设置此配置,并且没有提到简单的
self
分配将这些行放入
config/application.rb
或进入特定环境,例如进入 config/environments/development.rb之后,您可以像这样访问路由助手
Rails.application.config.action_mailer.default_url_options
Rails.application.default_url_options
Firstly setup Action Mailer default url options (like in the most of the answers)
And after that assign that options to Application. Initial question is exactly about this config! Not about the mailer:) It is strange that question is old but not all answers setup this config and didn't mention simple
self
assignmentPut such lines into
config/application.rb
or into specific environment, for example intoconfig/environments/development.rb
After that you can access to routes helpers like this