Jacob Relkin
Jacob Relkin

Reputation: 163248

scope equivalent in rails 2.3.x?

Is there a way to generate a group of routes under an admin scope without having to create a new physical directory (like namespace requires you to).

I know that in Rails 3 there is a scope method on the route mapper, and this appears to do what I want, but apparently it doesn't exist in Rails 2.3.x

My goal is to have a route like this: "/admin/products" map to "app/controllers/products_controller, not "app/controllers/admin/products_controller".

Is there any way to accomplish this in Rails 2.3.x?

Upvotes: 1

Views: 600

Answers (2)

Stéphan Kochen
Stéphan Kochen

Reputation: 19943

It appears to be not well documented, but namespace is actually a very simple wrapper for with_options. It sets the :path_prefix, :name_prefix, and :namespace options, of which I believe you only want the first, so:

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

I'm going through this from reading the code. It looks like :name_prefix is used to give named routes a prefix, and :namespace is used to actually look in subdirectories.

Upvotes: 2

Michael Sepcot
Michael Sepcot

Reputation: 11385

Sure, you need to use :name_prefix and :path_prefix to get to what you want:

ActionController::Routing::Routes.draw do |map|
  map.with_options :name_prefix => 'admin_', :path_prefix => 'admin' do |admin|
    admin.resources :products
  end
end

Will yield routes:

    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"}

Upvotes: 4

Related Questions