浙江省高等学校教师教育理论培训

微信搜索“毛凌志岗前心得”小程序

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

http://www.elharo.com/blog/software-development/web-development/2005/12/08/post-vs-put/

chinse version 易懂http://www.cnblogs.com/stalwart/archive/2010/05/15/1735971.html

very good good article 

http://www.artima.com/lejava/articles/why_put_and_delete.html

 

http://www.google.com/url?sa=t&source=web&cd=12&ved=0CB4QFjABOAo&url=http%3A%2F%2Fwww.w3.org%2FProtocols%2Frfc2616%2Frfc2616-sec9.html&rct=j&q=bottlepy%20method%20POST%20PUT%20DEL&ei=IzGCTK6RCZCgsQPC4Mj3Bw&usg=AFQjCNFxHgjZFQcPNUxGVI1QQ-61VBbiWA&sig2=2PsuLirrF5HvdB3vVMLfBQ&cad=rja

 

在rails中应用的文章

http://my.oschina.net/jing31/blog/6580?catalog=40347

我觉得不错,

有一些分析,我略作笔记:

接下来我们在此基础上来实现一下RESTful版本的CRUD。

为什么要采用RESTful风格:designing controller and action is chaos

 

HTTP methods (RFC 2616)

POST -> Create 

GET -> Read 

PUT -> Update 

DELETE -> Delete

 

Remove actions from URL, and we have simple named route.

/events/create -> POST /events

/events/show/1 -> GET /events/1

/events/update/1 -> PUT /events/1

/events/destroy/1 -> DELETE /events/1

 

CRUD-based action names get things simpler

create  -> POST 

show -> GET 

PUT -> update

delete -> DELETE

运行一下看看吧~

总结一下HTTP请求跟action的4-7关系表:

 

其实HTML规格只支持GET/POST,不支持PUT和DELETE方法的,rails在生成HTML的时候偷偷的做了一些处理。

我们写的代码是这样的:

  1. <%##event_path默认是GET,删除需要指定:method##%>  
  2. <%= link_to 'delete', event_path(event), :method => :delete %>  

生成的HTML是这样的:

 

  1. <a onclick="var f = document.createElement('form'); f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;var m = document.createElement('input'); m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method'); m.setAttribute('value', 'delete'); f.appendChild(m);var s = document.createElement('input'); s.setAttribute('type', 'hidden'); s.setAttribute('name', 'authenticity_token'); s.setAttribute('value', 'TwKvS1qYjCaO2RQ3QlCdo3K57NfH0yTqKShmMk8jjI0='); f.appendChild(s);f.submit();return false;" href="/events/1">delete</a>  

当点击的时候,实际上在页面上生成了下面内容:

 

  1. <form id="edit_events_1" method="post" action="/events/1">  
  2. <input type="hidden" value="put" name="_method"/>  
  3. ....  
  4. </form>  

其实各种浏览器之间还有一些关于DELETE和PUT一些支持或者不支持的不统一现象,这些问题Rails处理掉了,我们不需要关注了。

另外,XmlHttpRequest(Ajax request)定义了GET/POST/PUT/DELETE/HEAD/OPTIONS

不过不是所有浏览器都支持。

posted on 2010-09-04 20:06  lexus  阅读(701)  评论(0编辑  收藏  举报