【发布时间】:2014-03-19 01:53:02
【问题描述】:
以下帖子基于 Rails 4。
我目前正在寻找关于多个嵌套资源(超过 1 个)的最佳实践,以及选项 shallow:true。
最初在我的路线中,有这样的:
resources :projects do
resources :collections
end
相关的路线是:
project_collections GET /projects/:project_id/collections(.:format) collections#index
POST /projects/:project_id/collections(.:format) collections#create
new_project_collection GET /projects/:project_id/collections/new(.:format) collections#new
edit_project_collection GET /projects/:project_id/collections/:id/edit(.:format) collections#edit
project_collection GET /projects/:project_id/collections/:id(.:format) collections#show
PATCH /projects/:project_id/collections/:id(.:format) collections#update
PUT /projects/:project_id/collections/:id(.:format) collections#update
DELETE /projects/:project_id/collections/:id(.:format) collections#destroy
projects GET /projects(.:format) projects#index
POST /projects(.:format) projects#create
new_project GET /projects/new(.:format) projects#new
edit_project GET /projects/:id/edit(.:format) projects#edit
project GET /projects/:id(.:format) projects#show
PATCH /projects/:id(.:format) projects#update
PUT /projects/:id(.:format) projects#update
DELETE /projects/:id(.:format) projects#destroy
我在documentation 中看到关于嵌套资源的限制:
资源的嵌套深度不得超过 1 级。
好的。然后,就像文档说的那样,我将在我的路线中使用“浅”。
shallow do
resources :projects do
resources :collections
end
end
相关的路线是:
project_collections GET /projects/:project_id/collections(.:format) collections#index
POST /projects/:project_id/collections(.:format) collections#create
new_project_collection GET /projects/:project_id/collections/new(.:format) collections#new
edit_collection GET /collections/:id/edit(.:format) collections#edit
collection GET /collections/:id(.:format) collections#show
PATCH /collections/:id(.:format) collections#update
PUT /collections/:id(.:format) collections#update
DELETE /collections/:id(.:format) collections#destroy
projects GET /projects(.:format) projects#index
POST /projects(.:format) projects#create
new_project GET /projects/new(.:format) projects#new
edit_project GET /projects/:id/edit(.:format) projects#edit
project GET /projects/:id(.:format) projects#show
PATCH /projects/:id(.:format) projects#update
PUT /projects/:id(.:format) projects#update
DELETE /projects/:id(.:format) projects#destroy
我看到的主要区别是集合的“展示”,这个特定的:
collection GET /collections/:id(.:format) collections#show
所以如果我是正确的,集合的显示操作的链接是:
<%= link_to 'Show", collection_path(collection)%>
并且应该返回如下内容:“http://example.com/collections/1”
但是!两件事:
- 这不起作用。我得到的是“http://example.com/projects/1”。
- 即使它工作正常,但实际上 IMO 很糟糕,因为我松散了 REST 基本原则,即“集合是项目的子项,那么 url 应该是“localhost/project/1/collections/1”
如果我失去了休息动作的巨大优势,我不明白浅薄的好处是什么。放开“表演”动作又有什么好处呢?我已经将此发布到 SO,但我得到的唯一评论是“这很正常”。我不认为这是从其余 API 中“删除”操作的正常行为?
是的,帮助者使用 shallow 可能很方便,但对其他人来说一点也不方便,你失去了“一个集合嵌套到一个项目,所以这反映在 URL 中”的所有兴趣.
我不知道是否还有其他方法可以做到这一点,shallow 确实允许帮助器具有更大的灵活性,但它符合 REST 是错误的。那么,有没有机会让“助手”工作(使用“nested3_path(collection)”而不是“nested1_nested2_nested3([nested1.nested2.nested3,nested1.nested2,nested1])”,并保持“网址部分“nested1/123/nested2/456/nested3/789”?
【问题讨论】:
-
你试过重启服务器让路由生效吗?根据文档
resources :posts, shallow: true do resources :comments end将产生resources :posts do resources :comments, except: [:show, :edit, :update, :destroy] end resources :comments, only: [:show, :edit, :update, :destroy]这听起来像你在做 -
确实需要重启服务器才能让路由生效。
标签: ruby-on-rails api rest ruby-on-rails-4 nested-routes