Wiley Professional Ruby on Rails 978-0-470-22388-8 用户手册
产品代码
978-0-470-22388-8
Chapter 1: Building Resources
4
The REST of the Stor y
I pledge right now that will be the only REST - related pun in the whole book (unless I think of a really
good one later on).
good one later on).
REST is another one of those tortured software acronyms — it stands for REpresentational State
Transfer. The basic idea dates back to the doctoral dissertation of Ray Fielding, written in 2000, although
it only started gaining traction in the Rails world in early 2006, when a couple of different plugins
allowed for a RESTful style within Rails. The functionality was rapidly moved to the Rails core and has
just as quickly become a very commonly used practice, especially for standard Create, Read, Update,
Delete (CRUD) style functionality.
Transfer. The basic idea dates back to the doctoral dissertation of Ray Fielding, written in 2000, although
it only started gaining traction in the Rails world in early 2006, when a couple of different plugins
allowed for a RESTful style within Rails. The functionality was rapidly moved to the Rails core and has
just as quickly become a very commonly used practice, especially for standard Create, Read, Update,
Delete (CRUD) style functionality.
What Is REST?
There are three different ways of thinking about REST as compared to a traditional Rails application:
Pages versus resources
Network protocols
Rails features
You ’ ll explore each of these in the following sections.
Pages versus Resources
The traditional view of data on the Web is action - oriented. A user performs an action on a page, usually
by just accessing the page, but sometimes by sending data as well. The server responds with data,
usually in HTML, but a pure web service is likely to send XML or JSON.
by just accessing the page, but sometimes by sending data as well. The server responds with data,
usually in HTML, but a pure web service is likely to send XML or JSON.
A RESTful application, in contrast, is viewed as a set of resources, each of which contains some data and
exposes a set of functions to the Web. The core of these functions is made up of the standard CRUD
actions, and the application programming interface (API) for the standard functions is supposed to be
completely consistent between resources. A resource can also define additional actions for itself.
exposes a set of functions to the Web. The core of these functions is made up of the standard CRUD
actions, and the application programming interface (API) for the standard functions is supposed to be
completely consistent between resources. A resource can also define additional actions for itself.
If this reminds you of the distinction between procedural programming and object - oriented
programming (OOP), with REST resources playing the part of objects, well then you ’ ve got the gist. One
difference is that using REST in Rails primarily changes the way in which the user accesses your data
because it changes the URL structure of your site, but the data itself will be largely unaffected, whereas
an object - oriented design does affect the way your data itself is structured.
programming (OOP), with REST resources playing the part of objects, well then you ’ ve got the gist. One
difference is that using REST in Rails primarily changes the way in which the user accesses your data
because it changes the URL structure of your site, but the data itself will be largely unaffected, whereas
an object - oriented design does affect the way your data itself is structured.
Network Protocols
The signature feature of a REST - based web application is the use of HTTP access methods as critical data
when determining what to do in response to a request. HTTP defines four different methods for
requesting data (and eight methods overall). Many of us learned this fact in a beginning HTTP book or
network course and promptly filed the information under “ trivia that might win a bet someday, in a
bizarre set of circumstances. ” Only two of these methods are in general use — nearly every server since
the days of Mosaic has only used
when determining what to do in response to a request. HTTP defines four different methods for
requesting data (and eight methods overall). Many of us learned this fact in a beginning HTTP book or
network course and promptly filed the information under “ trivia that might win a bet someday, in a
bizarre set of circumstances. ” Only two of these methods are in general use — nearly every server since
the days of Mosaic has only used
GET
for getting information out of the server and
POST
for putting
❑
❑
❑
❑
❑
c01.indd 4
c01.indd 4
1/30/08 4:02:21 PM
1/30/08 4:02:21 PM