Professional Documents
Culture Documents
Jean-Baptiste Escoyez & Nicolas Jacobeus FOSDEM 08 Ruby on Rails DevRoom 24 February 2008
Frailers.net
Outline
REST
REST
REST
What is REST?
Its about communication between computers Its about designing the architecture of your applications
REST applies some constraints to the architecture of your application e interface is the same for humans and computers
(by the way: REST means Representational State Transfer... I think)
e concept of resource
Resource = thing exposed by the system to the outside world Everything is a resource
http://www.frailers.net/users/1 http://www.frailers.net/users/1/memberships
is is a DevRoom about
, right ?
REST in Rails
(Not on Rails !)
REST in Rails
Addressability: RESTful routes Representation independence: respond_to Nesting resources Developing REST clients ...and some tasty Rails sugars
REST in Rails
Addressability : Routes
RESTless VERB HREF
POST GET POST ????
/users/create /users/1 /users/1/update /users/1/delete
Addressability : Routes
map.resources :users
Resource
/users /users /users/1 /users/1 /users/1
Verb
GET POST GET PUT DELETE
Action
index create show update destroy
A RESTful controller has 7 standard actions Each controller deals with a resource (user) and its collection (list of users)
Based on:
HTTP Accept Header Format extension
http://www.frailers.net/articles/24/comments/1.html http://www.frailers.net/articles/24/comments/1.js
Nested resources
http://www.frailers.net/articles/24/comments/1 http://www.frailers.net/articles/24/author ActionController::Routing::Routes.draw do |map| map.resources :articles do |article| article.resources :comments article.resource :author end end
Helpers
link_to @article form_for @comment do |f| ... end redirect_to @article
To sum up...
Using REST in Rails is good lightweight controllers API given for free Rails is opinionated implementation of REST is not perfect
Best practices
Best practices
Best Practices
Disclaimer!
Design methodology
Knowing Rails resources API is great, but not sucient e big question:
Dont be tempted!
Rails allows extra, custom methods to be added to controllers, if you really need them But youll lose all what you were trying to do in the rst place (no uniform interface, etc.) I have never needed that (except, maybe...) If you do need that, its probable that youd better rethink your architecture
Best Practices Design methodology
We keep in the database the state of the shopping cart for each user:
/users/21/shopping_cart_items
ank you!
POST /lunch_eatings