4

I have problem with shallow nested resources. I my routes there is a big tree of nested resources but one of them have to be non-shallow. Simply example:

  resources :days, shallow: true do
    resources :meals, shallow: true do
      resources :ingredients, shallow: false
      resource :recipe, shallow: true
    end
  end

I would like to have days resources. Inside of it shallow meals resources. A inside of meals non-shallow ingredients resources. Because ingredients do not have an unique id. And moreover recipe resource. The problem is that shallow: false do not work! This could be found in Rails guide:

You can also specify the :shallow option in the parent resource, in which case all of the nested resources will be shallow:

So I get such routes for ingredients:

    meal_ingredients GET    /meals/:meal_id/ingredients(.:format)     ingredients#index
                     POST   /meals/:meal_id/ingredients(.:format)     ingredients#create
 new_meal_ingredient GET    /meals/:meal_id/ingredients/new(.:format) ingredients#new
     edit_ingredient GET    /ingredients/:id/edit(.:format)           ingredients#edit
          ingredient GET    /ingredients/:id(.:format)                ingredients#show
                     PATCH  /ingredients/:id(.:format)                ingredients#update
                     PUT    /ingredients/:id(.:format)                ingredients#update
                     DELETE /ingredients/:id(.:format)                ingredients#destroy

But I would like to have something like this:

GET    /meals/:meal_id/ingredients
POST   /meals/:meal_id/ingredients
GET    /meals/:meal_id/ingredients/new
GET    /meals/:meal_id/ingredients/:id/edit
GET    /meals/:meal_id/ingredients/:id
PATCH  /meals/:meal_id/ingredients/:id
PUT    /meals/:meal_id/ingredients/:id
DELETE /meals/:meal_id/ingredients/:id

Any ideas?

pablo
  • 384
  • 2
  • 5
  • 17
  • I should mention: if I put as option `shallow: false` in meals resources everything work the same way (not expected way). – pablo Oct 06 '13 at 00:17
  • Just a question. Are you sure about resources :days, shallow: true? may be just resources :days? – CodeGroover Oct 06 '13 at 03:33
  • Also you need to follow a rule 'Resources should never be nested more than 1 level deep.' (http://weblog.jamisbuck.org/2007/2/5/nesting-resources) – CodeGroover Oct 06 '13 at 03:41
  • @CodeGroover, I do not need to follow the rule. I would like to, but with such model I can not. And days resources are part of bigger resource tree. So I've put `shallow:` option on each to present it precisely. In real developing I put `shallow: true` option only on a root of tree of nested resources. – pablo Oct 06 '13 at 20:30

0 Answers0