以下职位是基于on Rails的4轨道4 [最佳做法]嵌套资源和浅:真
实际上,我在寻找有关的多个嵌套资源良好的最佳实践(大于1),和选项浅:真。
首先在我的路线,有这样的:
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
我有关嵌套资源的限制的文件中读取资源不应该嵌套超过1级。
来源:http://guides.rubyonrails.org/routing.html#limits-to-nesting 好的。然后,就像文件说的那样,我会在我的路线中使用“浅”。
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”
但是! 2件事:
- 这是行不通的。我得到的是“http://example.com/projects/1”。 WTF?
- 即使是工作,它实际上是非常糟糕的,因为我失去了REST基本是说:“收藏是项目的孩子,那么URL应该是‘本地主机/项目/ 1 /收藏/ 1’
我不明白什么是浅薄的利益,如果它是放松休息行动的巨大优势。什么是兴趣?什么是放松“显示”行动的兴趣?我已经发布到SO,但唯一的评论我得到的是“这是正常的东西。”跆拳道?在这是一个正常的行为,以“从其他API删除”行动?
我转载了一个中性项目的问题,以确保我没有做有问题,并发生同样的问题。所以,对于辅助者来说,使用浅度可能会比较方便,但对于其他人来说,这并不方便,因为您放弃了“一个集合嵌套到一个项目中的所有兴趣,所以这反映在URL中”。
我不知道是否有另一种方法可以做到这一点,浅的确允许助手具有更多的灵活性,但它是错误的,它是休息兼容的。那么,是否有机会让“帮助者”工作(拥有“nested3_path(collection)”而非“nested1_nested2_nested3([nested1.nested2.nested3,nested1.nested2,nested1])”,并保持“ url部分“并保持”nested1/123/nested2/456/nested3/789?
谢谢!
您是否尝试过重新启动服务器以使路由生效? 据商务部 '资源:帖子,浅:真正做 资源:评论 end' 会产生 '资源:帖子做 资源:评论,除了:[:显示,编辑,:更新, :destroy] end 资源:评论,只:[:显示,:编辑,:更新,:销毁]' 你好像在做什么 – jamesy829
确实必须重启服务器才能使路由生效。 – 0112