我想知道嵌套资源的传统方法是什么。根据指南,不应该有超过1个嵌套资源,如果是这样,他们建议使用shallow: true
。
但是,在这种情况下,我有:
在我的routes.rb
我知道不建议这样做:
resources :studies do
resources :hospitals, shallow: true do
resources :events
end
end
但这基本上就是我想要的:
/studies/1/hospitals/3/events/new
它应该需要一个:patient_id
(事件资源需要它作为一个字段,我不会感觉做好?patient_id=45
作为参数)。
我创建了一个手动资源,如下所示:
get '/events/new/:study_id/:hospital_id/:patient_id' => 'events#new', as: 'new_event'
这样做很好,我的参数很好 - 这是避免深度嵌套资源的建议方法吗?
感谢您的帮助。
答案 0 :(得分:0)
我会打破它们,所以你没有那么多的筑巢。像这样:
resources :studies do
resources :hospitals, shallow: true
end
resources :hospitals, only: [] do
resources :events, shallow: true
end
就发送patient_id而言,我会将其作为表格的一部分发送到帖子正文中。希望这会有所帮助。