REST and Rails
Note: This post is intended as a supplement to WTF Just Happened? A Quick Tour of Your First Rails App.
REST and Rails
REST is an approach to communications that is often used in the development of Web services. It is one of the defining features of Rails.
HTTP Access Methods
One of the main features of a REST-based web application is the use of HTTP access methods (also called verbs) when determining how to respond to a request.
HTTP defines four different methods for requesting data:
Only two of these methods are in general use:
GET for getting information from the server, and
POST for putting information onto the server. The other two methods are
A RESTful application uses all of these methods as a meaningful part of the Web action. A
GET request results in a
show action, the
DELETE request triggers the
destroy action, the
PUT request triggers the
update action, and the
POST request triggers the
Because HTML forms only handle
POST, Rails remains RESTful by wrapping the
DELETE link inside a small
POST form with a hidden field that Rails then decodes on the server end.
A RESTful application is viewed as a set of resources, each of which contains some data and exposes a set of functions to the Web. Within Rails, you do not explicitly define a class called a
Resource. Instead, a resource emerges from the interaction of a
Controller and a
Model with some magic in the route-mapping that ties them together.
In Rails, there are seven standard resource methods for requesting data:
These will be covered in more detail in MVC and Rails.
You can find an example of how these four HTTP verbs and seven resource methods are implemented in MVC - Controller.