Ruby-on-rails 如何在每个环境的基础上设置 config.action_controller.default_url_options = {:host = '#''}
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5691727/
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
How to set config.action_controller.default_url_options = {:host = '#''} on per environment basis
提问by trying_hal9000
Right now I'm using this which works for the development host, but I have to manually change the {:host => ""} code when I move to production.
现在我正在使用它,它适用于开发主机,但是当我转向生产时,我必须手动更改 {:host => ""} 代码。
post.rb
后.rb
def share_all
url = Rails.application.routes.url_helpers.post_url(self, :host => 'localhost:3000')
if user.authentications.where(:provider => 'twitter').any?
user.twitter_share(url)
end
end
I'd like to use this and then define the default_url_options per environment:
我想使用它,然后为每个环境定义 default_url_options:
post.rb
后.rb
def share_all
url = Rails.application.routes.url_helpers.post_url(self)
if user.authentications.where(:provider => 'twitter').any?
user.twitter_share(url)
end
end
I've tried adding this to my config/environments/development.rb but I still get the "Missing host to link to! Please provide :host parameter or set default_url_options[:host]" error
我已经尝试将它添加到我的 config/environments/development.rb 但我仍然收到“缺少要链接的主机!请提供 :host 参数或设置 default_url_options[:host]”错误
development.rb
发展.rb
config.action_controller.default_url_options = {:host => "localhost:3000"}
And I even tried it this way:
我什至这样试过:
development.rb
发展.rb
config.action_controller.default_url_options = {:host => "localhost", :port => "3000"}
EDIT:
编辑:
I've now also followed this and still the same error guide http://edgeguides.rubyonrails.org/action_controller_overview.html#default_url_options
我现在也遵循了这个并且仍然是相同的错误指南http://edgeguides.rubyonrails.org/action_controller_overview.html#default_url_options
application controller
应用控制器
class ApplicationController < ActionController::Base
protect_from_forgery
include ApplicationHelper
def default_url_options
if Rails.env.production?
{ :host => "example.com"}
else
{:host => "example1.com"}
end
end
end
This is driving me crazy, what am I missing here???
这让我发疯,我在这里错过了什么???
回答by trying_hal9000
Okay I figured it out the correct way to write it is
好吧,我想出正确的写法是
Rails.application.routes.default_url_options[:host] = 'localhost:3000'
:)
:)
回答by Joshua Pinter
Inherit your Application's default_url_optionsfrom ActionMailer.
继承你的应用是default_url_options从ActionMailer。
You want to keep things as DRY as possible so, ideally, you don't want to hard code your host and port in multiple places for the same environment, unless your ActionMaileractuallyuses a different host and port than the rest of your Application.
您希望尽可能保持 DRY,因此,理想情况下,您不想在同一环境的多个位置对主机和端口进行硬编码,除非您ActionMailer实际使用的主机和端口与Application.
To set the default_url_optionsfor your entire Application, simply add the following line to your config/environment.rbfile (changing MyAppto your app's name):
要default_url_options为整个设置Application,只需将以下行添加到您的config/environment.rb文件中(更改MyApp为您的应用程序名称):
# 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
This will fix your problem and automatically set your Application's default_url_optionsto the same as your config.action_mailer.default_url_options:
这将解决您的问题并自动将您的Application'sdefault_url_options设置为与您的相同config.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"}
回答by Ryan Bigg
You have to restart your server before the changes to this file takes effect.
在对此文件所做的更改生效之前,您必须重新启动服务器。
回答by Ashok Reddy Reddem
config.action_mailer.default_url_options = { :host => "your host" }
config.action_mailer.default_url_options = { :host => "你的主机" }
for instance your host localhost:3000
例如你的主机 localhost:3000
you can put this in test.rb, development.rb, production.rb files host could be different from environment to environment
您可以将其放在 test.rb、development.rb、production.rb 文件中,主机可能因环境而异
回答by Sean Griffin
I know this is an old thread, but I ran into this with Ruby 2.6.3 and Rails 5.2.3. The behavior I was seeing was basically that every path I added would fail with Error during failsafe response: undefined method 'empty?' for nil:NilClass. In production it worked fine, but in my development environment, I would get the error mentioned above.
我知道这是一个旧线程,但我在 Ruby 2.6.3 和 Rails 5.2.3 中遇到了这个问题。我看到的行为基本上是我添加的每条路径都会以Error during failsafe response: undefined method 'empty?' for nil:NilClass. 在生产中它运行良好,但在我的开发环境中,我会收到上面提到的错误。
The fix for me was add this to controllers/application_controller.rb:
对我的修复是将其添加到controllers/application_controller.rb:
def default_url_options
if Rails.env.production?
Rails.application.routes.default_url_options = { host: "www.production-domain.com", protocol: 'https' }
elsif Rails.env.development?
Rails.application.routes.default_url_options = { host: 'localhost:3000', protocol: 'http' }
end
end
I was then able to run my development environment on my local.
然后我就可以在本地运行我的开发环境了。

