为什么Ruby on Rails行动“破坏”未命名为“删除”?

编程入门 行业动态 更新时间:2024-10-23 08:37:03
本文介绍了为什么Ruby on Rails行动“破坏”未命名为“删除”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

CRUD原则定义了持久数据的四个基本操作:

The CRUD principle defines the four basic operations on persistent data:

  • 创建,
  • 阅读,
  • 更新,
  • 删除。
  • Create,
  • Read,
  • Update,
  • Delete.

HTTP谓词也使用DELETE字。

HTTP verbs also use the DELETE word.

为什么 Rails中的默认路由使用销毁一词来表示与HTTP动词相对应的操作 DELETE ?

Why does the default routing in Rails use the word "destroy" for the action corresponding to the HTTP verb DELETE?

推荐答案

Rails使用4种标准方法(动词),即:

Rails uses 4 standard methods(verbs), namely:

  • GET
  • POST
  • PUT
  • DELETE
  • GET
  • POST
  • PUT
  • DELETE

除此之外它有7个RESTful动作:

Besides it has 7 RESTful actions:

  • index
  • new
  • 创建
  • 编辑
  • 更新
  • show
  • destroy
  • index
  • new
  • create
  • edit
  • update
  • show
  • destroy

Rails从不使用与相应动作相同的动词。路由到动作destroy可以通过控制器中的相应操作执行多个DELETE。

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.

您可能会对此railsguide感兴趣: guides.rubyonrails/routing.html

This railsguide might be of interest to you: guides.rubyonrails/routing.html

解释

浏览器通过使用特定HTTP请求URL来请求Rails页面方法,如GET,POST,PUT和DELETE。每种方法都是对资源执行操作的请求。资源路由将许多相关请求映射到单个控制器中的操作。

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可以访问show,index,new或edit操作。他们都读取数据,但行动本身肯定不一样。关于DELETE请求也可以这么说。此请求通过控制器处理,并且可以在操作中具有不同的实现。您可能想要销毁帖子,但它可能意味着您要退出用户会话。只有一个名为delete的动作才能通过控制器证明与之相关的可能性。

Now, imagine we have a HTTP GET request, which means you want to read/retrieve data. If the action would have the same name as the verb, GET in this case, it would be overly simplistic. GET can give access to show, index, new or edit actions. They all read data, but the actions themselves are definitely not the same. The same could be said about the DELETE request. This request is processed through the controller and can have different implementations within actions. It might be you want to destroy a post, but it might as well mean you want to log out of your user session. Only having an action called delete would not justify the possibilities related to it, through the controller.

修改

如果您想了解更多有关浏览器请求的信息处理后,你可以阅读一些关于Rails使用的M(odel)V(iew)C(ontroller)模型的信息:

If you want to know more about how requests from the browser are processed, you could read some information about the M(odel)V(iew)C(ontroller)-model that Rails uses:

www.youtube/watch?v=3mQjtk2YDkM&noredirect=1

和:

betterexplained/articles/intermediate-rails-understanding-models-views-and-controllers/

报价来自此链接:

浏览器发出请求,例如mysite/video/show/15 Web服务器(mongrel,WEBrick等)收到请求。它使用路由来找出要使用的控制器:默认路由模式是/ controller / action / id,如config / routes.rb中所定义。

The browser makes a request, such as mysite/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.

意味着您的初始请求将通过网络服务器进行翻译和处理,并且必须通过控制器定义正确的路由,其中​​包含诸如destroy之类的restful操作。

Meaning your initial request will be translated and processed through the webserver and the correct route has to be defined through the controller, where the restful action, such as destroy, is located.

在Rails的早期,只有2个动词,即GET和POST(因为不支持PUT和DELETE,后续版本的rails通过添加PUT解决)并且通过隐藏变量进行删除。销毁操作的名称从未改变,因为请求和操作是两回事。

In the early days of Rails, there were only 2 verb's, namely GET and POST (since PUT and DELETE are not supported, which later versions of rails resolved by adding PUT and DELETE through hidden variables. The name of the destroy action never changed, since request and actions are two different things.

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

此报价可能会引起您的兴趣:

This quote may be of further interest:

因为路由器使用HTTP谓词和URL来匹配入站请求,所以有四个URL映射到七种不同的操作。

"Because the router uses the HTTP verb and URL to match inbound requests, four URLs map to seven different actions."

http://guides.ruby onrails/routing.html

更多推荐

为什么Ruby on Rails行动“破坏”未命名为“删除”?

本文发布于:2023-10-16 05:47:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1496673.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:命名为   Ruby   Rails

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!