Ruby on rails Rails 4嵌套资源,但不公开父级的RESTful路由?
我刚开始学习Ruby on Rails,并在一个简单的站点上工作,该站点具有以下设置:Ruby on rails Rails 4嵌套资源,但不公开父级的RESTful路由?,ruby-on-rails,rest,routes,nested-routes,Ruby On Rails,Rest,Routes,Nested Routes,我刚开始学习Ruby on Rails,并在一个简单的站点上工作,该站点具有以下设置: resources :categories do resources :products end resources :products do resources :features end 但是,我不想将url公开给products\u controller /products(.:format) prod
resources :categories do
resources :products
end
resources :products do
resources :features
end
但是,我不想将url公开给products\u 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\u controller公开restful路径,是否有其他解决方法?您可以通过使用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
谢谢你,成功了!我想可能有特殊的属性,我可以设置,以获得我需要的,但我想这是唯一的办法!据我所知,这是我过去的做法。如果有什么特别的方法,我很想知道。