简体   繁体   English

Ruby on Rails - 在REST API中区分复数与单数资源

[英]Ruby on Rails - differentiating plural vs singular resource in a REST API

I'm working on building the URLs for my REST API before I begin writing any code. 在开始编写任何代码之前,我正在为REST API构建URL。 Rails REST magic is fantastic, but I'm slightly bothered the formatting of a URL such as: Rails REST魔术太棒了,但我对URL的格式有点困扰,例如:

http://myproject/projects/5

where Project is my resource and 5 is the project_id. Project是我的资源,5是project_id。 I think if a user is looking to retrieve all of their projects, then a respective HTTP GET http://myproject/projects makes sense. 我想如果用户想要检索他们的所有项目,那么相应的HTTP GET http://myproject/projects是有意义的。 However if they're looking to retrieve information on a singular resource, such as a project, then it makes sense to have http://myproject/project/5 vs http://myproject/projects/5 . 但是,如果他们希望检索单个资源(例如项目)上的信息,那么使用http://myproject/project/5 vs http://myproject/projects/5是有意义的。 Is it best to avoid this headache, or do some of you share a similar concern and even better - have a working solution? 最好是避免这种头痛,或者你们中的一些人有同样的担忧甚至更好 - 有一个有效的解决方案吗?

Rails (3) has a lot of conventions when it comes to singular vs plural. Rails(3)在单数与复数方面有很多约定。 For example, model classes are always singular ( Person ), while the corresponding tables are always plural ( people ). 例如,模型类总是单数( Person ),而相应的表总是复数( people )。 (For example, Person.all maps to select * from people .) (例如, Person.all映射以select * from people 。)

For routes, there's a concept of a singular resource as well as a plural resource. 对于路线,有一个单一资源的概念以及一个复数资源。 So if you did resource :account then you would get paths like /account for the default path or /account/edit for a path to a form to edit the account. 因此,如果您使用resource :account那么您将获得路径,例如/account用于默认路径或/account/edit用于表单路径以编辑帐户。 (Note that Rails uses /account with a PUT method to actually update the account. /account/edit is a form to edit the account, which is a separate resource from the account itself.) If you did resources :people , however, then you would get paths like /people , /people/1 , and /people/1/edit . (请注意,Rails使用带有PUT方法的/account来实际更新帐户PUT /account/edit是一个用于编辑帐户的表单,这是一个与帐户本身不同的资源。)如果你做了resources :people然而, resources :people那么你会得到像/people/people/1/people/1/edit这样的路径。 The paths themselves indicate whether there can only be one instance of a given type of resource, or whether there can be multiple instances distinguished by some type of identifier. 路径本身指示是否只能存在给定类型资源的一个实例,或者是否可以存在由某种类型的标识符区分的多个实例。

I agree, go with the flow. 我同意,顺其自然。 Consider how the URL forms a hierarchy. 考虑URL如何形成层次结构。

The root of your website is where you start to access anything. 您的网站的根目录是您开始访问任何内容的地方。

/projects/ narrows it down to only projects, not anything else. / projects /将其缩小到只有项目,而不是其他任何东西。 From projects you can do lots of things, /list, /index/, /export, etc... the /id limits things even further. 从项目中你可以做很多事情,/ list,/ index /,/ export等... / id进一步限制了事情。

At each / the scope of what do becomes narrower, and I think it makes sense. 在每个/范围变得更窄,我认为这是有道理的。

Further programming is all about arbitrary rules. 进一步的编程都是关于任意规则的。 Indexs starting at 1 vs 0, and so on. 索引从1开始对0,依此类推。 Anyone working with your urls will sort things out in short order. 任何使用您的网址的人都会在短时间内解决问题。

There are cases where a singular path to a resource is helpful. 在某些情况下,资源的单一路径是有用的。 If your resource ids are non-numeric user defined names then routing clashes are possible. 如果您的资源ID是非数字用户定义的名称,则可以进行路由冲突。 Example: 例:

/applications/new --> create a new application or show user's application named new? / applications / new - >创建一个新的应用程序或显示用户名为new的应用程序?

In this situation you can choose to limit the user input to avoid the clash, or, this can be worked around by overwriting the default Rails 3 behavior: 在这种情况下,您可以选择限制用户输入以避免冲突,或者,这可以通过覆盖默认的Rails 3行为来解决:

class ActionDispatch::Routing::Mapper
  module Resources
    RESOURCE_OPTIONS  << :singular_resource
    class Resource
      def member_scope
        @options[:singular_resource] ? "#{singular}/:id" : "#{path}/:id"
      end

      def nested_scope
        @options[:singular_resource] ? "#{singular}/:#{singular}_id" : "#{path}/:#{singular}_id"
      end
    end
  end
end

Then when specifying a new resource route: 然后在指定新资源路由时:

resources :applications, :singular_resource => true

Which will generate the routes: 这将生成路线:

    GET     /applications
    GET     /applications/new
    POST    /applications
    GET     /application/:id
    GET     /application/:id/edit
    PUT     /application/:id
    DELETE  /application/:id

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

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