2014-12-11 5 views
3

Я только начал изучать Ruby On Rails и работает на простой сайт, который имеет следующие настройки:Rails 4 вложенных ресурсов, но не подвергает RESTful пути родителя?

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, но все равно будет показывать спокойный путь к продукту_контроллеру, так или иначе?

ответ

6

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

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 
+0

спасибо это сработало! Я думал, что может быть особенное свойство, которое я могу настроить, чтобы получить то, что мне нужно, но я думаю, что это единственный способ! – Stvyu

+0

Насколько я знаю, так я и делал это в прошлом. Если есть особый способ, я бы с удовольствием это узнал. –