The Basics of REST in Rails
REST fundamentals
HTTP supports 4 request (method) types:
- Post: Intended to create a resource
- Get: Intended to retrieve a resource
- Put: Intended to update a resource
- Delete: Intended to delete a resource
However browsers typically only support Post and Get requests. Put and Delete requests have been ignored and fulfilled by abusing Post and Get. The essence of REST lies in considering the web as providing information about resources, it proposes to change the way we view URL’s from actions to requests on a resource. Actions that were once part of the URL (i.e. /products/show/1), are now expressed in the method (HTTP GET /products/1).
Why should I use REST?
The key reasons appear to be:
- As an alternative to Web Services, Messaging etc. for manipulating a resource
- A consistent approach to resource discovery
Also consider that the cost of implementing RESTful requests in Rails is no more than conventional requests.
Rails Support
Rails accommodates the lack of browser support for Put and Delete with an additional request parameter that simulates the request type.
routes.rb is the key
The core of Rails’ REST support lies in routes.rb. As always, entries here map URL’s to controllers and actions.
The following entry allows the Product model to be accessed as a RESTful resource.
map.resources :products
These entries also generate methods used to assemble URL’s in views and controllers. Based on the previous example the following methods are generated:
- products_url, products_path
- product_url(id|product), product_path(id|product)
- new_product_url, new_product_path
- edit_product_url(id|product), edit_product_path(id|product)
They can be used as follows:
- Link to list all Products
link_to 'Products', products_path
- Form for creating a new product
form_tag(:product, :url => products_path) do
- Form for editing a product
form_tag(:product, :url => product_path(@product), :html => {:method => :put}) do - Link to delete a Product
link_to 'Delete', product_path(@product), :method => :delete
- Performing a redirect to view a Product
redirect_to product_url(@product)
Controllers respond via respond_to
REST allows clients to request different response types. Controller actions accommodate this using respond_to. Example:
def show
all_product = Products.find :all
respond_to do |format|
format.html
format.xml render(:xml => all_products.to_xml)
end
end
To issue a non-HTML RESTful request the URL must be a RESTful URL ending with .<desired type>. For example a XML request should end in .xml (i.e. METHOD GET /products/1.xml).
Scaffold support
Rails scaffolding supports the use of REST:
rake scripts/generate scaffold_resource <name> <field>:<type> <field>:<type>...
This command
- Creates: model, restful controller, views, migration with columns
- Edits: routes.rb for RESTful request support

September 18th, 2007 at 6:00 pm
Nice concise post on REST and Rails resources. Thanks.
October 7th, 2007 at 1:23 am
Very good…haven’t find one this clear
November 19th, 2007 at 4:13 pm
[...] The Basics of REST in Rails [...]