[英]How do I specify a default in a rails 2.3 route?
在我的Rails 2.3.11應用程序中,我想指定路由的默認格式為:xml。 根據文檔,我可以使用:defaults
map.connect '/myroute', :controller => 'mycontroller',
:action => 'myaction',
:defaults => {:format => :xml}
該文檔專門說這應該工作:
您也可以通過為:defaults選項提供哈希值來定義路由中的其他默認值。 這甚至適用於未在路徑中其他位置明確定義的參數。
但是,如果我這樣做,則會收到此錯誤:
/Users/simon/myproject/vendor/rails/actionpack/lib/action_controller/routing/builder.rb:107:in `assign_route_options':
format: No matching segment exists; cannot assign default (ArgumentError)
我看到有人為此提出了一張燈塔票 。 受訪者指出,它適用於資源,但不適用於命名路線; 管理員錯誤地將其標記為已修復,因為他已在資源上對其進行了測試。 哼
建議在其他地方這樣做:
map.connect '/myroute', :controller => 'mycontroller',
:action => 'myaction',
:format => :xml
但是如果我測試
assert_generates '/myroute', :controller => 'mycontroller',
:action => 'myaction'
我被告知沒有路由匹配:controller => 'mycontroller', :action => 'myaction'
我必須手工輸入格式,因此這不是默認設置。
如何在Rails 2.3路由中指定默認值? 我需要讓他們重新打開票證並真正修復錯誤嗎? 在Rails 3發布之后,是否有希望發生這種情況?
嗯,這很奇怪。 我在命名路由中使用了:defaults哈希,它對我有用。 您可以嘗試改用命名路由,看看是否可行嗎?
map.myroute '/myroute', :controller => 'mycontroller',
:action => 'myaction',
:defaults => {:format => :xml}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.