Representational State Transfer (REST) is a style of software architecture for distributed hypermedia systems such as the World Wide Web.
The ideas behind REST were formalized in Chapter 5 of Roy Fielding’s 2000 PhD dissertation.4 In a REST approach, servers communicate with clients using stateless connections: all the information about the state of the inter- action between the two is encoded into the requests and responses between them. Long-term state is kept on the server as a set of identifiable resources. Clients access these resources using a well-defined (and severely constrained) set of resource identifiers (URLs in our context). REST distinguishes the con- tent of resources from the presentation of that content. REST is designed to support highly scalable computing while constraining application architec- tures to be decoupled by nature.
Key REST principles
- Give every “thing” an ID
- Link things together
- Use standard methods
- Resources with multiple representations
- Communicate statelessly
REST架构中,使用GET PUT POST DELETE四个动作,来表示各种操作,操作的对象是应用程序中的资源。
Rails基于REST架构,并且实现了7种action方法:
- index:返回资源列表
- show :返回某个ID标识的资源
- new:新建资源页面
- create:创建资源
- edit:编辑资源页面
- update:更新某个ID标识的资源
- destroy:删除某个ID标的识资源
由于HTTP不能发起DELETE的请求,因此Rails对destroy进行了特殊处理。页面中使用Javascript将调用destroy的链接封装到一个表单内,通过POST发送。在表单内部会添加一个_method的属性,并将value设置为“delete”。
当Rails接收到_method参数时,就会忽略真正的HTTP方法,而使用该参数的值。