由於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