Firoz Ansari
Firoz Ansari

Reputation: 2515

Rails 3.2.1 - renaming routes

I am bit new with Rails (3.) and need your help to use renamed routes. I have following routes to display and search products.

  namespace :store do
    namespace :product do 
      resources :home
      resources :search
    end 
  end

And rake routes render following output for above resources:

  store_product_home_index GET    /store/product/home(.:format)                       store/product/home#index
                           POST   /store/product/home(.:format)                       store/product/home#create
    new_store_product_home GET    /store/product/home/new(.:format)                   store/product/home#new
   edit_store_product_home GET    /store/product/home/:id/edit(.:format)              store/product/home#edit
        store_product_home GET    /store/product/home/:id(.:format)                   store/product/home#show
                           PUT    /store/product/home/:id(.:format)                   store/product/home#update
                        DELETE    /store/product/home/:id(.:format)                   store/product/home#destroy
store_product_search_index GET    /store/product/search(.:format)                     store/product/search#index
                           POST   /store/product/search(.:format)                     store/product/search#create
  new_store_product_search GET    /store/product/search/new(.:format)                 store/product/search#new
 edit_store_product_search GET    /store/product/search/:id/edit(.:format)            store/product/search#edit
      store_product_search GET    /store/product/search/:id(.:format)                 store/product/search#show
                           PUT    /store/product/search/:id(.:format)                 store/product/search#update
                        DELETE    /store/product/search/:id(.:format)                 store/product/search#destroy

Instead of having path like /store/product/home, I wanted to rename as /products/home.

So modified routes should look like following:

  store_product_home_index GET    /products/home(.:format)                       store/product/home#index
                           POST   /products/home(.:format)                       store/product/home#create
    new_store_product_home GET    /products/home/new(.:format)                   store/product/home#new
   edit_store_product_home GET    /products/home/:id/edit(.:format)              store/product/home#edit
        store_product_home GET    /products/home/:id(.:format)                   store/product/home#show
                           PUT    /products/home/:id(.:format)                   store/product/home#update
                        DELETE    /products/home/:id(.:format)                   store/product/home#destroy
store_product_search_index GET    /products/search(.:format)                     store/product/search#index
                           POST   /products/search(.:format)                     store/product/search#create
  new_store_product_search GET    /products/search/new(.:format)                 store/product/search#new
 edit_store_product_search GET    /products/search/:id/edit(.:format)            store/product/search#edit
      store_product_search GET    /products/search/:id(.:format)                 store/product/search#show
                           PUT    /products/search/:id(.:format)                 store/product/search#update
                        DELETE    /products/search/:id(.:format)                 store/product/search#destroy

Note that I am using Rails 3.2.1.

Any help you can provide is greatly appreciated.

Upvotes: 0

Views: 470

Answers (1)

alex
alex

Reputation: 5002

As I understand you just want the /store namespace to be removed?

replace

 namespace :store do
    namespace :product do 
      resources :home
      resources :search
    end 
  end

with

namespace :product do 
  resources :home
  resources :search
end 

if you want to keep the namespace for structural reasons try

 namespace :store,:path => "" do
    namespace :product do 
      resources :home
      resources :search
    end 
  end

hope this helps!

Upvotes: 1

Related Questions