我已经在谷歌上搜索了大约90分钟,仍然没有答案。我在哪里设置default_url_options?我已经设置了config.action_mailer.default_url_options来解决其他地方的这个错误,但现在我在尝试使用RSpec规范内的URL helper时得到了这个错误。我不知道它在哪里期望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而不是路径。

什么好主意吗?


当前回答

你可以把host作为参数传递给URL helper:

listing_url(listing, host: request.host)

其他回答

Rails.application.routes.default_url_options(主持人):= ' localhost: 3000

在发展中。Rb / test。Rb,可以更简洁如下:

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

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

上面的答案对我不起作用,至少不是我想要的。 我意识到 Config.action_mailer.default_url_options ={主机:"localhost",端口:3000} 安装设备后。 希望对有同样问题的人有所帮助。

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

Config.action_mailer.default_url_options ={:主机=> "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

在路上的某个地方。rb:)

以防有人发现这个搜索有关ActiveStorage的错误:

如果你有一个控制器动作,你想用本地磁盘服务生成upload-urls等(很可能在测试环境中),你需要在控制器中包含ActiveStorage::SetCurrent以允许blob。Service_url_for_direct_upload才能正常工作。