Take a REST with Rails

Posted by robl

As my talk for our local Ruby User Group meetup , I’ve prepared a presentation about the new RESTful features in Rails 1.2. It covers changes to routing, generators and gives a quick overview of the REST architectural style, where it originated and how it applies to Rails. To demonstrate these concepts, a RESTful blog is created. This blog is then used as part of a final example that uses ActiveResource to consume RESTful resources.

Download “Take a REST with Rails” (odp) (pdf)

Download the sample code blog.tar.gz and slurpr.tar.gz

Post-Talk update

I was pleased with the talk and there were some interesting questions at the end so I thought I’d compile a set of ‘more complete’ answers and document them here …

1. Does the number of routes increase from using the restful approach ?

It depends largely if you originally relied on the default controller/action/id model or if you try and create more intuitive routes. I think the REST approach provides a more intuitive URI structure, so if you prefer this model then I think the size of your routing file will be reduced by using the REST approach. See here for more interesting stats on this subject.

2. Which elements of simply_restful have been integrated into Rails 1.2 ?

My original answer wasn’t quite correct as I was confused by the map.resources nested block invocation. As part of the integration into the rails core the functionality of map.resources was modified to include this nested block form. So the answer is map.resources (with modifications), generators and route helpers. For more information see here.

3. How does Rails manage the fact that all browsers don’t support all the HTTP methods required (specifically PUT/DELETE) ?

With a hack ! Using the form_for helper with one of the new route helpers will cause a hidden field ‘_method’ to be added with the ‘real’ action specified whilst the form itself will use POST to submit. Rails then checks the _method variable and actually performs the request routing using the HTTP method specified in _method.

4. What about service discovery ?

I thought that WSDL could be used for service discovery and it turns out that this is true, at least as far as WSDL 2.0 is concerned !. This is more of a REST question than a REST on Rails question and I think the (quickest) explanation I could find was :

What’s the alternative to WSDL ? There was a proposal for a Web Resource Definition Language, sortof a RESTful WSDL. http://www.prescod.net/rest/wrdl/wrdl.html . That seems somewhat moribund. Potentially, the answer could be WSDL 2.0, which would leverage the REST-friendly features of SOAP 1.2. There doesn’t seem to be much demand among customers of WSDL 2.0 for this, however, so the working group hasn’t gotten around to working on this yet. More generally, I think the REST people would say that if you have the URI of a RESTful service and the schemas of the representations being transferred, you don’t need WSDL, since the basic actions and message exchange patterns are assumed to be those baked into HTTP. (from a comment on Adam Bosworth.)

I really enjoyed the debate and questions from the evening, although I’ll probably run through a few more practice sessions before I try and code in front of a room of 20 people again :)

Comments

Leave a response