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

1

6

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?

Bedstraw answered 11/12, 2014 at 14:39 Comment(0)
S
15

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
Skiplane answered 11/12, 2014 at 14:50 Comment(2)
thank you it worked! I thought there might be special property that I can set to get what I need but I guess this is the only way!Bedstraw
Not to my knowledge, it's the way I've done it in the past. If there is a special way I'd love to know it.Skiplane

© 2022 - 2024 — McMap. All rights reserved.