如何根据环境设置 config.action_controller.default_url_options = {:host = '#''}
现在我正在使用适用于开发主机的这个,但我必须手动更改 {:host =>;当我转向生产时“”}代码。
post.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
我想使用它,然后定义每个环境的 default_url_options:
post.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
我尝试将其添加到我的 config/environments/development.rb 但我仍然得到“缺少要链接的主机!请提供:host 参数或设置 default_url_options[:host]" 错误
development.rb
config.action_controller.default_url_options = {:host => "localhost:3000"}
我什至尝试过这种方式:
development.rb
config.action_controller.default_url_options = {:host => "localhost", :port => "3000"}
编辑:
我现在也遵循了这个,但仍然是相同的错误指南 http://edgeguides.rubyonrails.org/action_controller_overview.html#default_url_options
应用程序控制器
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
这让我发疯,我在这里错过了什么???
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.
post.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:
post.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
development.rb
config.action_controller.default_url_options = {:host => "localhost:3000"}
And I even tried it this way:
development.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
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???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
我知道这是一个旧线程,但我在 Ruby 2.6.3 和 Rails 5.2.3 中遇到了这个问题。我看到的行为基本上是我添加的每个路径都会失败,并出现
Error during failuresafe response: undefined method 'empty?'对于 nil:NilClass
。在生产中它工作得很好,但在我的开发环境中,我会收到上面提到的错误。对我来说,修复方法是将其添加到
controllers/application_controller.rb
:然后我就可以在本地运行我的开发环境。
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.The fix for me was add this to
controllers/application_controller.rb
:I was then able to run my development environment on my local.
我自己尝试在 rake 任务中生成 URL 时遇到了这个问题。这绝对是不明显的,而且其中许多解决方案都会起作用。我的解决方案是从 @joshua-pinter 起飞,但在环境文件中完成。例如,我的
development.rb
如下所示:对
development.rb
进行了适当的更改。Just ran into this problem myself trying to generate a URL in a rake task. It's definitely non-obvious and many of these solutions will work. My solution is a take off from @joshua-pinter's but done in the environment file. For example my
development.rb
looks like:with the appropriate changes made to
production.rb
.对我来说有效的是
,如果您已经在配置文件中设置了端口,那么仅更改 [:host] 就会导致错误
For me whats worked is
because if you already set a port in the config files then changing the [:host] only will result in an error
在每个环境文件中设置特定的 url 选项
,然后在 config/routes.rb 文件中设置默认 url 选项
Set your specific url options in each environment file with
And then set the default url options in your
config/routes.rb
file with好吧,我发现正确的写法是
:)
Okay I figured it out the correct way to write it is
:)
从
ActionMailer
继承应用程序的default_url_options
。您希望尽可能保持干燥,因此,理想情况下,您不想在同一环境的多个位置对主机和端口进行硬编码,除非您的
ActionMailer
实际上 使用与应用程序
的其余部分不同的主机和端口。要为整个
Application
设置default_url_options
,只需将以下行添加到config/environment.rb
文件中(更改MyApp< /code> 到您应用的名称):
这将解决您的问题并自动将您的
Application
的default_url_options
设置为与您的相同config.action_mailer.default_url_options
:Inherit your Application's
default_url_options
fromActionMailer
.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
ActionMailer
actually uses a different host and port than the rest of yourApplication
.To set the
default_url_options
for your entireApplication
, simply add the following line to yourconfig/environment.rb
file (changingMyApp
to your app's name):This will fix your problem and automatically set your
Application
'sdefault_url_options
to the same as yourconfig.action_mailer.default_url_options
:您必须重新启动服务器才能使此文件的更改生效。
You have to restart your server before the changes to this file takes effect.
config/environments/development.rb(任何其他环境,相同)
添加此行以及您想要的主机
config/environments/development.rb (any other environment, same)
add this row with host that you want
config.action_mailer.default_url_options = { :host =>; "your host" }
例如您的主机 localhost:3000
您可以将其放入 test.rb、development.rb、 Production.rb 文件中 主机可能因环境而异
config.action_mailer.default_url_options = { :host => "your host" }
for instance your host localhost:3000
you can put this in test.rb, development.rb, production.rb files host could be different from environment to environment
我通常是这样解决这个问题的:
首先,从
rails new
自动生成的文件中正确设置您的config/environments/Production.rb
(和其他环境):然后,返回到您的 config/application.rb 并设置为从此 ActionMailer 配置继承:
This is how I usually go about this problem:
First, properly set your
config/environments/production.rb
(and other environments) from therails new
auto-generated file:Then, go back to your
config/application.rb
and set to inherit from this ActionMailer configuration: