Jim Mitchener
Jim Mitchener

Reputation: 9003

Rails 3 Resource: Share Custom Actions with Nested Resources

I have 2 resources events and patients

resources :events do
  collection do
    get :upcoming
    get :missed
  end
end

resources :patients do
  resources :events # does not have upcoming or missed
end

Is there a way to have the events nested resource within the patients definition share the custom collection members from the primary events resource without having to define them again?

Upvotes: 2

Views: 315

Answers (1)

clyfe
clyfe

Reputation: 23770

You can define a method in your routes file and can call it each time, as such keep DRY.

def events_actions
  collection do
    get :upcoming
    get :missed
  end
end

resources :events do
  events_actions
end

resources :patients do
  resources :events do
    events_actions
  end
end

Or take things even further:

def resources_events
  resources :events do      
    collection do
      get :upcoming
      get :missed
    end
  end
end

resources_events

resources :patients do
  resources_events
end

Upvotes: 1

Related Questions