我已经在谷歌上搜索了大约 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 而不是路径。
有任何想法吗?
您需要在每个环境中添加以下行:
config.action_mailer.default_url_options = { :host => "yourhost" }
这样,它可以在所有环境中工作,并且可能因环境而异。例如:
发展.rb
config.action_mailer.default_url_options = { :host => "dev.yourhost.com" }
测试.rb
config.action_mailer.default_url_options = { :host => "test.yourhost.com" }
生产.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
中的某处 :)
listing_url(listing, :host => "whatever.com")
。
config.domain = 'staging.myapp.com'
(当然,为每个环境替换一个正确的域名)。然后在 routes.rb
中,我可以自由使用 default_url_options host: Rails.application.config.domain
,它可以在任何环境中工作。别客气。
default_url_options Rails.application.config.action_mailer.default_url_options
。
主机应该在每个环境的配置文件中指定。例如:
config/environments/development.rb
请参阅 this question 和 this question。
设置 default_url_options 以使用您的 action_mailer.default_url_options。
在您的每个环境文件(例如 development.rb
、production.rb
等)中,您可以指定 default_url_options
用于 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_options
设置为使用您在相应环境文件(development.rb
、production.rb
等)中为 action_mailer
定义的内容。
为了尽可能保持干燥,请在您的 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
匹配:
$ MyApp::Application.config.action_mailer.default_url_options
#=> {:host=>"lvh.me", :port=>"3000"}
$ MyApp::Application.default_url_options
#=> {:host=>"lvh.me", :port=>"3000"}
向 @pduersteler 致敬,感谢他带领我走上这条道路。
config/environment.rb
是让我的邮件程序在控制台中工作的关键。谢谢!
Rails.application.default_url_options = Rails.application.config.action_mailer.default_url_options
当您使用任何 listing_url
方法时,将返回完整的 URL(不是正常的相对 URL)。这就是为什么 rails 要求您提供主机来计算整个 URL。
你怎么能告诉rails主机?您可以通过以下几种方式做到这一点:
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/*
因为当您测试引擎时,它就是 rails正在测试。
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
方法
MyEngine::Engine.config.action_mailer.default_url_options = Rails.application.config.action_mailer.default_url_options
MyEngine::Engine.routes.default_url_options = Rails.application.config.action_mailer.default_url_options
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
有趣的是,设置 config.action_mailer.default_url_options
对我没有帮助。此外,在我觉得不属于它的地方乱搞与环境无关的设置对我来说并不令人满意。此外,我想要一个在 sidekiq/resque 工作者中生成 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 >= 3.2.x。
config/environment.rb
文件中添加一行来执行此操作。并添加了详细的答案:stackoverflow.com/a/48529627/293280 感谢您向我指出这个方向。
您始终可以将 host 作为参数传递给 URL 帮助程序:
listing_url(listing, host: request.host)
listing_url(listing, host: request.host, port: 3000)
转到配置/环境/test.rb
Rails.application.routes.default_url_options[:host] = 'localhost:3000'
以防万一有人发现此搜索有关 ActiveStorage 的错误:
如果您有一个控制器操作,您想使用本地磁盘服务生成上传 URL 等(很可能在测试环境中),您需要在控制器中设置 include ActiveStorage::SetCurrent
以允许 blob.service_url_for_direct_upload
正常工作。
上面的答案对我不起作用,至少不是我想要的。我在安装设备后意识到 config.action_mailer.default_url_options = { host: "localhost", port: 3000 }
。希望它会帮助有同样问题的人。
您可以在应用程序控制器中设置默认 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”替换为我的服务器网址。
我在教程中忽略的是这一行:
重新启动开发服务器以激活配置后...
所以我的答案是关闭服务器然后重新打开。
在路由中添加 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'
我通过将 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]
不想改变其他环境的行为,所以我使用了:
发展.rb
Rails.application.configure do
...
end
Rails.application.default_url_options = Rails.application.config.action_mailer.default_url_options
在 Rails 6 中工作。
default_url_options Rails.application.config.action_mailer.default_url_options
并且这样做了,正如另一位评论者所建议的那样