代码之家  ›  专栏  ›  技术社区  ›  Jacob Relkin

rails 2.3.x中的等效范围?

  •  1
  • Jacob Relkin  · 技术社区  · 15 年前

    是否有方法在 admin 范围 没有 必须创建一个新的物理目录(比如 namespace 需要你这么做)。

    我知道在Rails 3中有一个 scope 方法在路由映射器上,这似乎是我想要的,但是显然它不存在于Rails 2.3。

    我的目标是有一条这样的路线: "/admin/products" 映射到 "app/controllers/products_controller , "app/controllers/admin/products_controller" .

    在Rails2.3.x中有什么方法可以实现这一点吗?

    2 回复  |  直到 15 年前
        1
  •  4
  •   Michael Sepcot    15 年前

    当然,你需要用 :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
  •  2
  •   Stéphan Kochen    15 年前

    似乎没有很好的记录,但是 namespace 实际上是一个非常简单的包装 with_options . 它设定了 :path_prefix , :name_prefix ,和 :namespace 选择,我相信你只想要第一个,所以:

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

    我在读代码的时候就把这个弄清楚了。看起来像 :名称前缀 用于为命名路由赋予前缀,以及 :命名空间 用于实际查找子目录。