Question

I've been deep nesting my resources and now discover that this should be avoided. Shallow nesting seems to be the answer but I'm struggling on how to properly correct the second level deep nesting. Let me show you my routes file as I think that will make it clearer(I'm using Rails 4):

OLD:

  resources :members do
    resources :emails
    resources :events do
      resources :items
    end
  end

NEW so far:

resources :members, shallow: true do
  resources :emails
  resources :events
end

My issue is I don't understand where to put the :items now? I thought it should be nested under the :events for the :index, :new, and :create actions?

What am I missing, I'm sure its something silly that I'm just not getting yet, please excuse my ignorance and I think you in advance for any help!!!

Mark

Was it helpful?

Solution

All you need to do is, update the routes as below:

  resources :members, shallow: true do
    resources :emails
    resources :events, shallow: true do
      resources :items
    end
  end

This will create shallow routes for items. Only for :index, :new, and :create actions you would get nested routes prefixed with /events/:event_id.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top