简体   繁体   English

Ruby on Rails URL中的资源映射(RESTful API)

[英]Resource mapping in a Ruby on Rails URL (RESTful API)

I'm having a bit of difficulty coming up with the right answer to this, so I will solicit my problem here. 我有点困难想出正确的答案,所以我会在这里征求我的问题。 I'm working on a RESTFul API. 我正在研究RESTFul API。 Naturally, I have multiple resources, some of which consist of parent to child relationships, some of which are stand alone resources. 当然,我有多个资源,其中一些资源包括父母与子女之间的关系,其中一些资源是独立的资源。 Where I'm having a bit of difficulty is figuring out how to make things easier for the folks who will be building clients against my API. 我遇到一些困难的地方在于弄清楚如何让那些将根据我的API构建客户的人们更容易。

The situation is this. 情况就是这样。 Hypothetically I have a 'Street' resource. 假设我有'街头'资源。 Each street has multiple homes. 每条街都有多个房屋。 So Street :has_many to Homes and Homes :belongs_to Street. So Street:has_many to Homes and Homes:belongs_to Street。 If a user wants to request an HTTP GET on a specific home resource, the following should work: 如果用户想要在特定的主资源上请求HTTP GET,则以下内容应该有效:

http://mymap/streets/5/homes/10

That allows a user to get information for a home with the id 10. Straight forward. 这允许用户获得id为10的家的信息。直接前进。 My question is, am I breaking the rules of the book by giving the user access to: 我的问题是,我是否通过授予用户访问权限来违反本书的规则:

http://mymap/homes/10

Technically that home resource exists on its own without the street. 从技术上讲,家庭资源本身就没有街道。 It makes sense that it exists as its own entity without an encapsulating street, even though business logic says otherwise. 有意义的是,它存在于没有封装街道的自己的实体中,即使业务逻辑另有说法。

What's the best way to handle this? 处理这个问题的最佳方法是什么?

EDIT! 编辑! In spirit of becoming a good StackOverflow citizen, I've come back with a supported code block for how to implement they above. 为了成为一名优秀的StackOverflow公民,我回过头来看一下如何在上面实现它们的支持代码块。

map.resources :streets,
              :has_many => :homes
              :shallow => true

This will create both types of routes that I was looking for. 这将创建我正在寻找的两种类型的路线。

If your Home records can only belong to one Street, then the relationship won't be confused when you're examining a Home individually. 如果您的家庭记录只能属于一个街道,那么当您单独检查一个家庭时,这种关系不会混淆。 You'll still be able to back-track to the associated Street record for whatever reason. 无论出于何种原因,您仍然可以回溯到相关的街道记录。

It's in situations where you have a many-to-many relationship that de-nesting your REST structure can get you into trouble. 在您拥有多对多关系的情况下,对REST结构进行解嵌可能会让您遇到麻烦。 If a particular record only makes sense in a particular context, and you remove that context, obviously there is confusion. 如果特定记录仅在特定上下文中有意义,并且您删除了该上下文,则显然存在混淆。

I think in your particular case you may not need to implement both approaches, but instead go with the "flatter" approach that reduces the complexity of the URL. 我认为在您的特定情况下,您可能不需要实现这两种方法,而是采用“更平坦”的方法来降低URL的复杂性。

不,这是浅薄的路线工作和使用很多。

I really like this approach. 我真的很喜欢这种方法。 I recomend to read it. 我建议你阅读它。 In short this article says that you shouldn't nest your resources more then 1 level. 简而言之,本文说您不应该将资源嵌套到1级以上。 And if it is possible that nested resource can be shallowed, then do it. 如果嵌套资源有可能被浅化,那就去做吧。

In one of my applications I really messed things up with nested resources. 在我的一个应用程序中,我真的用嵌套资源搞砸了。 I go even to 3 or 4 deep and it become a nightmare... 我甚至走到3或4深,这成了一场噩梦......

Nesting is really nice if it makes things simpler. 如果让事情变得简单,嵌套真的很棒。 If not, give it up! 如果没有,请放弃!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM