RailsConfEurope: Advanced RESTful Rails
Next session was Advanced RESTful Rails by Ben Scofield of Viget Labs, which turned out to be an interesting in depth talk about advanced REST concepts and how to implement them in Rails. This talk and the talk RESTful Everything – Towards a Complete Resource-oriented Workflow yesterday, reignited my interest for REST (it kind of faded out a bit lately).
REST is basically about two things: resources (which don’t have to map to tables / models) and representations (each resources can have different representations e.g. book/1, users/1/favoritebooks/2).
RAILS implements REST quite nicely. Ben discussed some more advanced REST techniques that aren’t know very well or that are not used very often:
- singleton resources (e.g. for session, profile, password (for current user), search)
- namespaces (e.g. to group admin stuff)
- nesting
- polymorphism (e.g. “map.resources :user, :has_many => [:readings]” and “map.resources :book, :has_many => [:readings]“.
- custom routes (changing url’s and getting to views with a nice url)
There are a couple of plugins that try to DRY up RESTful controllers:
- resource_this
- resources_controller
- make_resourceful
Ben Scofield finds them too much magic and prefers to see the actual REST code. I tend to agree with him.
Modelling advice
Ben’s tips on how to get from a messy domain to a nice set of RESTful resources:
- identify resources
- select HTTP verbs to expose
Some tips:
- when in doubt, add a resource (you can also roll it up later = easier)
- respect the middleman (join models)
- resources != models
Examples:
- homepage /dashboard might be the index of a resource (look what it’s function is)
- Search: over a single resource then add filter with query param to index. Multiple resources? Then use a new (non-db) model (e.g. Searcher). Most cases you should use GET.
- Wizards (see slides)
- Bulk actions: Rails REST doesn’t handle bulk actions on resources well. Just override the normal map.resource with a custom route! (see slides)
- Transactions: can also be moddeled RESTfully (
- Administration: also no problem (use map.namespace, http basic auth)
Stumbling blocks (for Rails)
- Rails helpers (e.g. link_to … method => ‘delete’) are not very accesible
- default routing (in bottom of routes.rb) overrides all RESTful routest (they are not protected anymore). So remove it
- unused actions (e.g. formatted variations)
- collections (see previous comment on Bulk Actions)
- ARes Avalanche (always check server log)
At the end Ben also mentioned a couple of framework that might be worth looking at in the context of REST: Sinatra, Waves and Halcyon.
After the presentation I asked Ben for some advice for refactoring my controllers to be more RESTful. He gave some really good tips. Thanks for that Ben! If I implemented the RESTful controllers I might write a separate post on how I did that. I also bought the book RESTful webservices by Leonard Richardson and Sam Ruby, so it won’t be a long before I will be a REST guru too
resources
http://www.viget.com/extend
http://www.culann.com







