Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 4 nested resources but not exposing the RESTful routes of parent?

I just started learning Ruby on Rails and working on a simple site that has the following setup:

  resources :categories do
    resources :products
  end

  resources :products do
    resources :features
  end

however I don't want to expose url to products_controller

/products(.:format)                                  products#index
/products(.:format)                                  products#create
/products/new(.:format)                              products#new
/products/:id/edit(.:format)                         products#edit
/products/:id(.:format)                              products#show
/products/:id(.:format)                              products#update
/products/:id(.:format)                              products#update
/products/:id(.:format)                              products#destroy

I just need routes that look like the following

/products/:product_id/features(.:format)             features#index
/products/:product_id/features(.:format)             features#create
/products/:product_id/features/new(.:format)         features#new
/features/:id/edit(.:format)                         features#edit
/features/:id(.:format)                              features#show
/features/:id(.:format)                              features#update
/features/:id(.:format)                              features#update
/features/:id(.:format)                              features#destroy 

I know the above routing can be done by marking shallow: true, but it would still expose restful path to products_controller, is there anyway around this?

like image 363
Stvyu Avatar asked Dec 11 '14 14:12

Stvyu


1 Answers

You can limit it to the actions you want by using either only or except. Using only with an empty array should remove the route.

  resources :categories do
    resources :products
  end

  resources :products, only: [] do
    resources :features
  end

So now if I rake routes

 category_products GET    /categories/:category_id/products(.:format)                                  products#index
                                       POST   /categories/:category_id/products(.:format)                                  products#create
                  new_category_product GET    /categories/:category_id/products/new(.:format)                              products#new
                 edit_category_product GET    /categories/:category_id/products/:id/edit(.:format)                         products#edit
                      category_product GET    /categories/:category_id/products/:id(.:format)                              products#show
                                       PATCH  /categories/:category_id/products/:id(.:format)                              products#update
                                       PUT    /categories/:category_id/products/:id(.:format)                              products#update
                                       DELETE /categories/:category_id/products/:id(.:format)                              products#destroy
                            categories GET    /categories(.:format)                                                        categories#index
                                       POST   /categories(.:format)                                                        categories#create
                          new_category GET    /categories/new(.:format)                                                    categories#new
                         edit_category GET    /categories/:id/edit(.:format)                                               categories#edit
                              category GET    /categories/:id(.:format)                                                    categories#show
                                       PATCH  /categories/:id(.:format)                                                    categories#update
                                       PUT    /categories/:id(.:format)                                                    categories#update
                                       DELETE /categories/:id(.:format)                                                    categories#destroy
                      product_features GET    /products/:product_id/features(.:format)                                     features#index
                                       POST   /products/:product_id/features(.:format)                                     features#create
                   new_product_feature GET    /products/:product_id/features/new(.:format)                                 features#new
                  edit_product_feature GET    /products/:product_id/features/:id/edit(.:format)                            features#edit
                       product_feature GET    /products/:product_id/features/:id(.:format)                                 features#show
                                       PATCH  /products/:product_id/features/:id(.:format)                                 features#update
                                       PUT    /products/:product_id/features/:id(.:format)                                 features#update
                                       DELETE /products/:product_id/features/:id(.:format)                                 features#destroy
like image 72
j-dexx Avatar answered Dec 01 '22 06:12

j-dexx