Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions


WDVL Newsletter

jobs.webdeveloper.com

e-commerce
Partner With Us















Developer Channel
FlashKit.com
JavaScript.com
JavaScriptSource
Developer Jobs
ScriptSearch
StreamingMediaWorld
Web Developer's Journal
Web Developer's Virtual Library
WebDeveloper.com
Webreference
Web Hosts
XMLfiles.com

internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


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.

HTTPCRUDUse Case
GETReadretrieving a resource
PUTUpdateupdating a resource
POSTCreatecreating a new resource
DELETEDeletedeleting 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

ActionResult
GET /customersretrieves list of customers
POST /customerscreates a new customer
GET /customers/35retrieves customer 35
PUT /customers/35updates customer 35
DELETE /customers/35deletes 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 URLCustomers Controller Action
GET /customersindex
POST /customerscreate
GET /customers/35show
PUT /customers/35update
DELETE /customers/35destroy

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


Up to => Home / Authoring / Tutorials / Ruby on Rails




Jupiter Online Media: internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and Jupiter Online Media

Jupitermedia Corporate Info


Legal Notices, Licensing, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers