2010-11-22 69 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用于实际查看子目录。