Вложенные ресурсы Rails 4, но не раскрывающие родительские маршруты RESTful?

Я только начал изучать 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, есть ли что-нибудь вокруг этого?


person Stvyu    schedule 11.12.2014    source источник


Ответы (1)


Вы можете ограничить его действиями, которые вы хотите, используя только или кроме. Использование только с пустым массивом должно удалить маршрут.

  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
person j-dexx    schedule 11.12.2014
comment
спасибо, это сработало! Я думал, что может быть специальное свойство, которое я могу установить, чтобы получить то, что мне нужно, но я думаю, что это единственный способ! - person Stvyu; 11.12.2014
comment
Насколько мне известно, это то, как я делал это в прошлом. Если есть особый способ, я хотел бы знать его. - person j-dexx; 11.12.2014