Archive for the 'REST' Category

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

RailsConfEurope: RESTful Everything – Towards a Complete Resource-oriented Workflow

Didn’t really know what to expect from this session, but it was really good (both content and presentation).

The session was titled RESTful Everything – Towards a Complete Resource-oriented Workflow and was presented by Ingo Weis.

abstract from the conference site:

“Rails’ RESTful routing facility provides developers with conventions for naming controllers and controller methods. However, Rails fails to keep up the RESTful momentum beyond controllers. This presentation is about all the good things that happen when picking up where Rails left off and establishing resource-oriented conventions for helper names and CSS classes.”

Ingo explained what is missing in Rails at the moment for a truly RESTful webapp and demonstrated a plugin he developed that complement the rails helpers for RESTful routing. I think that the helpers of this plugin produce very clear, readable and compact views. Another advantage is that they helpers produce really consistent html class and id attributes so that styling and attaching javascript behavior becomes a lot easier too. This way developers and designers get a common vocabulary (something we could really use at the company I work for). Really nice!

As a bonus, Ingo also showed a really nice way of making a google map accessible to users without Javascript: make a list of the locations with the geo microformat, select these locations with javascript and put them on a Google map. Really nice :)

I am definitely going to look into this plugin and this way of writing my view templates. Maybe hold an internal presentation for our designers too.
Resources:

slides: later on conference site
rails plugin: http://svn.ingoweiss.com/plugins/resourceful_views/tags/REL_0_1/resourceful_views/