REST and Rails
by Erik Andrejko
April 14, 2009
|
In this article Erik Andrejko will discuss Ruby on Rails and REST, a
powerful architecture for organizing web applications.
|
There are many different patterns of organizing a web
application or a web service. Once such pattern is a widely
used convention in Ruby on Rails: Representational State
Transfer (REST). REST is an architecture for designing both
web applications and application programming interfaces
(APIs) that use HTTP. Ruby on Rails has adopted REST as a
standard for organizing web applications and has been
designed to easily support development using a RESTful
architecture. It isn't necessary to always use a RESTful
architecture with Ruby on Rails but doing so offers many
advantages.
The REST Architecture
On a very high level, many web applications can be
thought of as being composed of a number of resources, and
for each of these resources the web application will support
some common actions: Create, Read, Update and Delete
(CRUD) actions.
Most web developers are familiar with the most common
HTTP methods of GET and POST but there are other less widely
used HTTP methods. In fact, there is direct correspondence
between HTTP methods and the CRUD actions. In a RESTful
architecture each resource of a web application is
associated with a unique URL and each resource is accessible
using the HTTP methods that correspond to CRUD actions.
| HTTP | CRUD | Use Case |
| GET | Read | retrieving a resource |
| PUT | Update | updating a resource |
| POST | Create | creating a new resource |
| DELETE | Delete | deleting a resource |
Unfortunately, most web browsers only support the HTTP
GET and POST methods, but the HTTP standard
specifies the PUT and DELETE methods as well. Ruby on Rails
provides a mechanism to allow browsers to support all of the
necessary HTTP methods to allow access to the full REST
architecture.
In a RESTful architecture each resource will be
identified with a unique URL and support some of the HTTP
methods of GET, POST, PUT and DELETE. For example if the
/customers URL is the location of the customer
resource then
| Action | Result |
GET /customers | retrieves list of customers |
POST /customers | creates a new customer |
GET /customers/35 | retrieves customer 35 |
PUT /customers/35 | updates customer 35 |
DELETE /customers/35 | deletes customer 35 |
Most of the details of the REST architecture is
implemented automatically behind the scenes in Ruby on Rails
by a simple configuration directive.
RESTful Routes
It is straightforward to implement a RESTful resource in
Ruby on Rails by using a special route. In Rails a route
connects a combination of a URL and an HTTP verb with a
particular controller action. All of the routes for the
application are stored in the config/routes.rb
file.
Routes can be configured in Ruby on Rails in many
different ways. One type of route found in almost all Rails
applications is the default route:
map.connect ':controller/:action/:id'
The default route connects every controller and action to
its default URL. For example, using this route the
show action of the Customers
controller is accessible from the
/customers/show URL. The customer id to show
may be specified with the URL
/customers/show/31 in which case that
params[:id] will equal 31 inside the
Customers controller.
Creating a RESTful routes is simpler as it will be
organized using a standard convention. To create all of the
RESTful routes connecting each HTTP method with the
corresponding Customers controller action it is
only necessary to add the following to the
route.rb file:
map.resources :customers
This will map each of the RESTful HTTP methods with the
corresponding controller action automatically. It is
assumed that the Customers controller will
implement the standard CRUD actions. Thus, a RESTful
controller in Rails should contain at least the following
actions:
class CustomersController < ApplicationController
def index
...
end
def show
...
end
def create
...
end
def update
...
end
def destroy
...
end
end
Each of these actions will then be called depending on
the HTTP method of the request. For example:
| HTTP Method and URL | Customers Controller Action |
GET /customers | index |
POST /customers | create |
GET /customers/35 | show |
PUT /customers/35 | update |
DELETE /customers/35 | destroy |
The map.resources command in the
route.rb will also generate named routes. A
named route is a method that can be used in place of the URL
of a resource inside of a view. For example, the URL
representing the customers resource is accessible using the
customers_path method. The URL for the
resource representing the customer stored in
@customer is accessible by using the
customer_path(@customer) method.
Creating Models
Getting Started with Ruby on Rails
Forms
|