I’m working on building the URLs for my REST API before I begin writing any code. Rails REST magic is fantastic, but I’m slightly bothered the formatting of a URL such as:
http://myproject/projects/5
where Project is my resource and 5 is the 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. 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. 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. For example, model classes are always singular (
Person), while the corresponding tables are always plural (people). (For example,Person.allmaps toselect * from people.)For routes, there’s a concept of a singular resource as well as a plural resource. So if you did
resource :accountthen you would get paths like/accountfor the default path or/account/editfor a path to a form to edit the account. (Note that Rails uses/accountwith aPUTmethod to actually update the account./account/editis a form to edit the account, which is a separate resource from the account itself.) If you didresources :people, however, then you would get paths like/people,/people/1, and/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.