CRUD principle定义了持久数据的四种基本操作:
- 创建,
- 阅读,
- 更新,
- 删除。
HTTP 动词也使用 DELETE 词。
为什么 default routing in Rails对 HTTP 动词 DELETE?
对应的操作使用单词“destroy”最佳答案
Rails 使用 4 种标准方法(动词),即:
- 获取
- 发布
- 放置
- 删除
此外它还有 7 个 RESTful Action :
- 索引
- 新
- 创建
- 编辑
- 更新
- 展示
- 摧毁
Rails 从不使用与相应 Action 相同的动词。路由到操作 destroy 可以通过 Controller 中的相应操作执行多个 DELETE。
您可能会对这个 railsguide 感兴趣: http://guides.rubyonrails.org/routing.html
解释
Browsers request pages from Rails by making a request for a URL using a specific HTTP method, such as GET, POST, PUT and DELETE. Each method is a request to perform an operation on the resource. A resource route maps a number of related requests to actions in a single controller.
现在,假设我们有一个 HTTP GET 请求,这意味着您想要读取/检索数据。如果操作与动词同名,在本例中为 GET,则将过于简单化。 GET 可以授予对显示、索引、新建或编辑操作的访问权限。都是读取数据,但 Action 本身肯定不一样。 DELETE 请求也是如此。此请求通过 Controller 处理,并且可以在操作中有不同的实现。可能是您想销毁帖子,但也可能意味着您想退出用户 session 。只有一个名为 delete 的操作并不能通过 Controller 证明与之相关的可能性。
编辑
如果您想了解更多关于如何处理来自浏览器的请求,您可以阅读一些关于 Rails 使用的 M(odel)V(iew)C(ontroller) 模型的信息:
http://www.youtube.com/watch?v=3mQjtk2YDkM&noredirect=1
和:
http://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/
引自此链接:
The browser makes a request, such as http://mysite.com/video/show/15 The web server (mongrel, WEBrick, etc.) receives the request. It uses routes to find out which controller to use: the default route pattern is “/controller/action/id” as defined in config/routes.rb.
这意味着您的初始请求将通过网络服务器进行转换和处理,并且必须通过 Controller 定义正确的路由, Controller 是 restful 操作(例如销毁)所在的位置。
早期的Rails只有2个动词,分别是GET和POST(因为不支持PUT和DELETE,后来的rails版本通过隐藏变量添加PUT和DELETE解决了这个问题。销毁 Action 的名字从未改变,因为请求和操作是两个不同的事物。
Actions || show || create || update || destroy
SQL || select|| create || update || delete
REST || get || post || post || post
Actions || show || create || update || destroy
SQL || select|| create || update || delete
REST || get || post || put || delete
这句话可能更有趣:
"Because the router uses the HTTP verb and URL to match inbound requests, four URLs map to seven different actions."
关于ruby-on-rails - 为什么 Ruby on Rails 操作 "destroy"没有命名为 "delete"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14730451/