jvoorhis | What’s New in Edge Rails: Restful Routes

Restful Routes talks about the new RESTful programming models that are now possible in Rails (now in Edge, soon to be in the released version).


In this case, the url /comments/1;approve would be created. The rationale is to use the path to the left of the semicolon for a resource’s identity, and to use the path to the right of the semicolon as a modifier. Frequently this modifier would be an operation that you would perform on the resource, such as approving a comment that requires moderation.


This isn't very restful. This modifier hack is just another way to force another verb into the mix.

The best way to be more RESTful would be to PUT a representation of the comment with the approval flag set to true. In REST, representations of resources are exchanged. Instead of referencing the URI of the comment, with a verb modifier stuck on the end, send along a representation of the whole comment.

Now, you're saying, "But how do I do that in Rails?" This is, of course, very difficult. Most of the fault lies with the XHTML spec, which has been updated to support full method selection in form actions, or the inability to send form data as XML. You don't really care about the representation format, so the current name=value pairs would work OK as the body of the PUT. But a lack of PUT as a form action? It's a RESTful crime.

Another way to be more RESTful would be to create a new resource, a collection that accepts ApproveComment documents. This is a document style web service, but minus the SOAP. This models the event that is "Approve this Comment". You can then POST this new even to the /comments/approval_queue. The representation would include the comment id you want to approve.

This intermediary resource is similar to why Rails has introduced :has_many :through. Those old many to many join tables are old and busted, and first class join models are the new hotness. They function to enrich the domain model and make relationships themselves a first class citizen. I think of these new documents, the ApproveComment document, as filling in the same type of gap.

The URI, /comments/1;approve, both identifies the noun and the verb. To me, that violates a central tenent of REST, in that you should only need the CRUD methods. I see no difference between /comments/approve/1 and /comments/1;approve when talking about the intended consequences of the user's request.

It's great to see Rails try to be more RESTful, don't get me wrong. They are doing some great work here. I'd like to see them take it even farther, and use only the HTTP verbs (GET, POST, PUT, DELETE) and make new documents (representations) for what would have been that URI modifier.

Popular posts from this blog

Lists and arrays in Dart

The 29 Healthiest Foods on the Planet

Converting Array to List in Scala