Rails Rails 将 action 名设计为 destroy 而请求却是 delete ?

ShiningRay · 2013年10月15日 · 最后由 doitian 回复于 2013年10月16日 · 4342 次阅读

新同学在这上面搞混了,我也不知道如何解释 rails 为什么这么设计

好问题!查查

要区分一下 HTTP 协议里的方法和 control 里的方法 前者常用到的:get、post、put、delete、head 等等 后者与前者的对应关系,在 guides 里有详细说明: http://guides.rubyonrails.org/routing.html#crud-verbs-and-actions

Rails uses 4 standard methods(verbs), namely:

  • GET
  • POST
  • PUT
  • DELETE

Besides it has 7 RESTful actions:

  • index
  • new
  • create
  • edit
  • update
  • show
  • destroy

Rails never uses the same verb as the corresponding action. Routing to the action destroy makes it possible to do more than a single DELETE, through the corresponding action in the controller.

http://stackoverflow.com/questions/14730451/why-the-ruby-on-rails-action-destroy-is-not-named-delete

问题,是不是应该修改为:为什么以 delete 方式发起请求,但对应的 action 却为 destroy .

因为 http 标准里面没有 destroy 的类型,只有 delete

这问题可以扩展为 为何 POST 对应的 action 是 create PUT 对应的 action 是 update DELETE 对应的 action 是 destroy?

DELETE 是一个抽象,Destroy 只是 DELETE 的一种形式...

#6 楼 @iBachue 是不是因为 CRUD 的惯例名称是:create read update destory

Destroy 只是一种 action 名称,Delete 是 HTTP 协议中请求方式的名称。两者是两码事!!

或许我没理解好问题!!

或许是作为面向对象方式考虑,创建对象是 create,而不是 insert,同理,销毁对象 destroy,而不是删除 delete

那为什么 post 请求对应 create 呢?为什么 ruby on rails 不叫 rails on ruby 呢?这种问题太多了

我能说这是约定吗?

@huacnlee 发的 SO 的回答,好像是在 HTTP verb DELETE 出现之前,Rails 就已经有 destroy action 了,虽然后来支持了新的 DELETE verb,但是 action 的名字还是保持不变。

需要 登录 后方可回复, 如果你还没有账号请 注册新账号