Rails 4嵌套资源但不暴露父级的RESTful路由?

时间:2014-12-11 14:39:11

标签: ruby-on-rails rest routes nested-routes

我刚刚开始学习Ruby on Rails并开发一个简单的网站,它具有以下设置:

  resources :categories do
    resources :products
  end

  resources :products do
    resources :features
  end

但我不想将网址公开给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

我只需要看起来如下的路线

/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 

我知道上面的路由可以通过标记shallow: true来完成,但它仍然会向products_controller公开restful路径,不管怎么说呢?

1 个答案:

答案 0 :(得分:10)

您可以使用only或except将其限制为所需的操作。仅使用空数组应删除路径。

  resources :categories do
    resources :products
  end

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

现在如果我耙路线

 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