【问题标题】:Rails 4 nested resources but not exposing the RESTful routes of parent?Rails 4嵌套资源但不暴露父级的RESTful路由?
【发布时间】:2015-02-10 02:05:07
【问题描述】:

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

  resources :categories do
    resources :products
  end

  resources :products do
    resources :features
  end

但是我不想将 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

我只需要如下所示的路线

/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 路径,还有这个问题吗?

【问题讨论】:

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


    【解决方案1】:

    您可以使用 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
    

    【讨论】:

    • 谢谢你成功了!我认为可能有一些特殊的属性可以设置来获得我需要的东西,但我想这是唯一的方法!
    • 据我所知,这是我过去的做法。如果有什么特别的方法我很想知道。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-31
    • 1970-01-01
    • 1970-01-01
    • 2016-04-05
    • 1970-01-01
    • 2011-05-23
    • 1970-01-01
    相关资源
    最近更新 更多