Rails 4 nested resources but doesn't expose RESTful to parent paths?

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

  resources :categories do
    resources :products
  end

  resources :products do
    resources :features
  end

      

however i dont want to show url 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 only need routes that look like this

/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 labeling shallow: true

, but it will still show a quiet path to products_controller, is it all around that anyway?

+3


source to share


1 answer


You can restrict it to the actions you want using only or excluding. 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

      

+7


source







All Articles