Rails 3资源:使用嵌套资源共享自定义操作

时间:2011-03-27 04:20:29

标签: ruby-on-rails ruby-on-rails-3

我有2个资源eventspatients

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

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

是否有办法让events定义中的patients嵌套资源与主要事件资源共享自定义集合成员,而无需再次定义它们?

1 个答案:

答案 0 :(得分:1)

您可以在路线文件中定义一个方法,并且每次都可以调用它,因此请保持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

或者更进一步:

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

resources_events

resources :patients do
  resources_events
end