由于ActionController :: Base#default_url_options已被弃用,我不知道如何在rails3中设置默认的url选项。默认的url选项不是静态的,而是取决于当前的请求。default_url_options和rails 3
http://apidock.com/rails/ActionController/Base/default_url_options
感谢, 科林
由于ActionController :: Base#default_url_options已被弃用,我不知道如何在rails3中设置默认的url选项。默认的url选项不是静态的,而是取决于当前的请求。default_url_options和rails 3
http://apidock.com/rails/ActionController/Base/default_url_options
感谢, 科林
要为当前请求使用这样的网址选项控制器:
class ApplicationController < ActionController::Base
def url_options
{ :profile => current_profile }.merge(super)
end
end
现在:配置=> CURRENT_PROFILE将automerge到路径/ URL参数。
示例路由:
scope ":profile" do
resources :comments
end
只要写:
comments_path
如果CURRENT_PROFILE已成立to_param为 '卢卡斯':
/lucas/comments
我认为首选的方法是现在告诉路由器来处理:
Rails.application.routes.default_url_options[:foo]= 'bar'
你可以把此行要么routes.rb
或初始化。无论你喜欢什么。如果值根据您的环境而改变,您甚至可以将其放入环境配置中。
但我怎么能使这依赖于当前的请求,所以例如访问实例变量? – gucki 2011-05-23 18:23:17
叶,它不是线程安全的。我通过自定义方法和Thread.current做了 – 2011-08-15 17:05:55
看起来这是Rails 3.2的。 – aceofspades 2012-03-19 18:34:56
这apidock.com链接是一种误导。不推荐使用default_url_options。
http://guides.rubyonrails.org/action_controller_overview.html#default_url_options
我觉得指南已经过时了,而不是apidoc。该方法不再适用于Rails 3.2。 – jpgeek 2012-09-18 05:06:08
对于Rails 3中明确,规范的方式来做到这一点是通过添加default_url_options
方法您ApplicationController
。
class ApplicationController < ActionController::Base
def default_url_options
{
:host => "corin.example.com",
:port => "80" # Optional. Set nil to force Rails to omit
# the port if for some reason it's being
# included when you don't want it.
}
end
end
我只是自己弄清楚了这一点,所以我知道它的工作原理。
http://guides.rubyonrails.org/v3.2.21/action_controller_overview.html#default_url_options
'的ActionController ::基地#default_url_options' [*不是*弃用(http://edgeguides.rubyonrails.org/action_controller_overview.html#default:
这是从轨道3指南适于-url选项)。 – Mohamad 2015-08-01 14:10:13