2010-11-22 68 views
1

有沒有辦法在admin範圍內生成一組路徑而不需要必須創建一個新的物理目錄(就像namespace要求你一樣)。範圍?

我知道,在Rails 3中存在的路由映射器scope方法,這似乎做我想做的,但顯然它不會在Rails的2.3.x版本存在

我的目標是有這樣的路線:"/admin/products" map to "app/controllers/products_controller不是"app/controllers/admin/products_controller"

有什麼辦法可以在Rails 2.3.x中完成這個任務嗎?

回答

4

當然,你需要使用: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"} 
2

這似乎是無據可查,但namespace實際上是一個非常簡單的包裝with_options。它集:path_prefix:name_prefix:namespace選項,而我相信你只想要第一個,所以:

map.with_options :path_prefix => 'admin/' do |admin| 
    admin.connect ':controller/:action' 
end 

我從閱讀的代碼要通過這一點。它看起來像:name_prefix用於給予命名路由一個前綴,並且:namespace用於實際查看子目錄。