简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM