开发者

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 mis开发者_如何学Gosed
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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜