轨道4 [最佳做法]嵌套资源和浅层:真 [英] Rails 4 [Best practices] Nested resources and shallow: true
问题描述
以下职位是基于on Rails的4
The following post is based on Rails 4.
我其实是在寻找有关的多个嵌套资源(大于1),选择浅了良好的最佳实践:真实。
I'm actually looking for a good best-practices about the multiple nested resources (more than 1), and the option shallow: true.
首先,在我的路线,有这样的:
First in my routes, there was this :
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
我对嵌套资源的限制文档中读取
资源不应该被嵌套超过10级深。
Resources should never be nested more than 1 level deep.
来源:<一个href=\"http://guides.rubyonrails.org/routing.html#limits-to-nesting\">http://guides.rubyonrails.org/routing.html#limits-to-nesting
好。然后,像文件说,我在我的路线会用浅。Source : http://guides.rubyonrails.org/routing.html#limits-to-nesting Ok. Then, like the documentation said, I'm gonna use "shallow" in my routes.
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
主要的区别我看到的是藏品的秀,这样一句:
The major difference I see is the "show" of collections, this one :
collection GET /collections/:id(.:format) collections#show
所以,如果我我是对的,链接的演出行动的集合是:
So if I I'm right, the link for the show action for a collection is :
<%= link_to 'Show", collection_path(collection)%>
和应该返回这样的事情: http://example.com/collections/1
and should return something like this : "http://example.com/collections/1"
但是!两件事情:
- 这是行不通的。我得到,而不是 http://example.com/projects/1 。 WTF?
- 即使是工作,它实际上是pretty糟糕了,因为我失去了REST基本的说:收藏是项目的孩子,那么URL应该是本地主机/项目/ 1 /收藏/ 1李>
- This is not working. I'm getting instead "http://example.com/projects/1". WTF ?
- Even if it was working, it's actually pretty bad because I loose the REST basic that say "Collection is child of project, then the url should be "localhost/project/1/collections/1"
我不明白什么是浅的利益,如果它是松散的休息行动的一大优势。有什么兴趣?什么是宽松的显示行动的兴趣?我已经张贴这对SO,但只有我得到的评论是这件事情正常。跆拳道?什么,这是一个正常的行为,删除,从REST API的行动?
I don't understand what is the interest of shallow if it's to loose the big advantage of Rest actions. What's the interest ? And what is the interest to loose the "Show" action ? I already posted this to SO, but the only comment i got is "It's something normal". WTF? In what this is a normal behavior to "remove" an action from the rest API ?
我转载这个问题上中立的项目,以确保我是不是做错了什么,而同样的问题发生。所以,是的,它可能是便于助手使用浅,但它不是在所有方便的休息,你失去的一切利益一个集合嵌套一个项目,所以这一点也反映在URL中。
I reproduced the problem on a neutral project, to be sure that I was not doing something wrong, and the same problem happened. So, yes, it may be convenient for the helpers to use shallow, but it's NOT AT ALL convenient for the rest, you loose all the interest of "one collection is nested to one project, so this is reflected in the URL".
我不知道是否有另一种方式来做到这一点,这是真的,浅让有关佣工更大的灵活性,但它是假的,它的其余部分兼容。那么,有没有任何机会获得助手的工作(它的pretty真棒有nested3_path(集合)而不是nested1_nested2_nested3([nested1.nested2.nested3,nested1.nested2,nested1]),并保持URL的一部分,并保持有nested1 / 123 / nested2 / 456 / nested3 / 789?
I don't know if there is another way to do this, it's true that shallow allow more flexibility about the helpers, but it's false that it's rest compliant. So, is there any chance to get the "helpers" working (it's pretty awesome to have "nested3_path(collection)" instead of "nested1_nested2_nested3([nested1.nested2.nested3, nested1.nested2, nested1])", and keeping the "url part" and keep having "nested1/123/nested2/456/nested3/789 ?
谢谢!
推荐答案
我不认为Rails的任何提供内置的方式有URL使用完整的层次(如
/项目/ 1 /收藏/ 2
),但也有快捷佣工(如collection_path
而不是project_collection_path
)。I don't believe that Rails offers any built-in way to have the URLs use the full hierarchy (e.g.
/projects/1/collections/2
) but also have the shortcut helpers (e.g.collection_path
instead ofproject_collection_path
).如果你真的想这样做,你可能会推出类似下面的自定义的助手:
If you really wanted to do this, you could roll out your own custom helper like the following:
def collection_path(collection) # every collection record should have a reference to its parent project project_collection_path(collection.project, collection) end
不过,这将是非常麻烦的每个资源做手工。
But that would be quite cumbersome to manually do for each resource.
我觉得用
浅
路线是最好的文档总结了背后的理念:I think the idea behind the use of
shallow
routes is best summed up by the documentation:,以避免深嵌套(如上建议)的一种方法是产生
父下范围的收集操作,以便获得感
层次结构,但不窝构件的动作。换一种说法,
只建立与信息提供给最小量的路由
唯一地识别所述资源One way to avoid deep nesting (as recommended above) is to generate the collection actions scoped under the parent, so as to get a sense of the hierarchy, but to not nest the member actions. In other words, to only build routes with the minimal amount of information to uniquely identify the resource
来源:<一个href=\"http://guides.rubyonrails.org/routing.html#shallow-nesting\">http://guides.rubyonrails.org/routing.html#shallow-nesting
因此,虽然因为每个资源都可以被唯一标识,你能走备份层次假设你的关联设置正确,这可能不是REST兼容的(像你说的),你是不是丢失任何信息。
So while this may not be REST-compliant (as you say), you aren't losing any information because each resource can be uniquely identified and you are able to walk back up the hierarchy assuming your associations are set up properly.
这篇关于轨道4 [最佳做法]嵌套资源和浅层:真的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!