REST in Rails

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方法,而使用该参数的值。

Also see http://www.infoq.com/articles/rest-introduction

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值