[英]scope equivalent in rails 2.3.x?
有沒有一種方法可以在admin
作用域內生成一組路由, 而不必創建新的物理目錄(如namespace
要求您這樣做)。
我知道在Rails 3中,路由映射器上有一個scope
方法,這似乎可以滿足我的要求,但是顯然它在Rails 2.3.x中不存在。
我的目標是要有一條這樣的路線: "/admin/products"
映射到"app/controllers/products_controller
, 而不是 "app/controllers/admin/products_controller"
。
在Rails 2.3.x中有什么方法可以做到這一點?
當然,您需要使用:name_prefix
和:path_prefix
來獲得所需的內容:
ActionController::Routing::Routes.draw do |map|
map.with_options :name_prefix => 'admin_', :path_prefix => 'admin' do |admin|
admin.resources :products
end
end
將產生路線:
admin_products GET /admin/products(.:format) {:controller=>"products", :action=>"index"} POST /admin/products(.:format) {:controller=>"products", :action=>"create"} new_admin_product GET /admin/products/new(.:format) {:controller=>"products", :action=>"new"} edit_admin_product GET /admin/products/:id/edit(.:format) {:controller=>"products", :action=>"edit"} admin_product GET /admin/products/:id(.:format) {:controller=>"products", :action=>"show"} PUT /admin/products/:id(.:format) {:controller=>"products", :action=>"update"} DELETE /admin/products/:id(.:format) {:controller=>"products", :action=>"destroy"}
它似乎沒有很好的文檔記錄,但是namespace
實際上是with_options
的非常簡單的包裝器。 它設置了:path_prefix
, :name_prefix
和:namespace
選項,我相信您只需要第一個,所以:
map.with_options :path_prefix => 'admin/' do |admin|
admin.connect ':controller/:action'
end
我正在通過閱讀代碼來解決這個問題。 看起來:name_prefix
用於給命名路由:name_prefix
前綴,而:namespace
用於實際在子目錄中查找。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.