简体   繁体   English

ruby rails web 请求响应生命周期

[英]ruby rails web request response lifecycle

I'm a novice in ruby on rails trying to understand the in-depth flow of a typical request/response life cycle in ruby on rails web application.我是 ruby​​ on rails 的新手,试图了解 ruby​​ on rails web 应用程序中典型请求/响应生命周期的深入流程。

I have googled for the info and have not found an answer which is complete or detailed to the level of DNS servers to dispatchers.我在谷歌上搜索了相关信息,但没有找到完整或详细到调度员的 DNS 服务器级别的答案。

The closest I got to a good explanation is at: http://brainspl.at/request_response.pdf .我最接近一个好的解释是: http : //brainspl.at/request_response.pdf

Can someone point me to a better or more detailed explanation?有人可以指出我更好或更详细的解释吗?

-Raviteja -Raviteja

So you are asking for rails request/response cycle and you already referred to a presentation which really describes it very well.因此,您要求使用 rails 请求/响应周期,并且您已经参考了一个演示文稿,它确实很好地描述了它。 So im assuming that you want to know it from a very high level and you need this concept totally for development.所以我假设你想从一个非常高的层次上了解它,并且你完全需要这个概念来进行开发。 Then here it is.然后就是这里了。 Im just trying to name the parts sequentially.我只是想按顺序命名这些部分。

  1. Route: Here you will draw the paths which will be used by the world to access your application.路线:在这里,您将绘制世界将用于访问您的应用程序的路径。 With a complete RESTful architecture, you need to define the hierarchy of your resources and define how a resource can be accessed to perform some action.使用完整的 RESTful 架构,您需要定义资源的层次结构并定义如何访问资源以执行某些操作。 If any request to your application doesnt match with any path in the routes file, it will not be processed.如果对您的应用程序的任何请求与路由文件中的任何路径都不匹配,则不会对其进行处理。 If any match occurs, it will find the corresponding controller and action and will call it.如果发生任何匹配,它将找到相应的控制器和动作并调用它。 At the time of calling, it will store all the request related data in params hash.在调用时,它将所有请求相关的数据存储在params哈希中。
  2. Before Filters: Now your application already know which controller#method is gonna process the request.过滤器之前:现在您的应用程序已经知道哪个控制器#method 将处理请求。 And it will check if there is anything configured to execute before calling that method.它会在调用该方法之前检查是否有任何配置要执行。 This is done by using before_filter .这是通过使用before_filter来完成的。 If found anything then those functions will be called first.如果找到任何内容,则将首先调用这些函数。
  3. Method Execution: After executing all the before_filter methods in a particular sequence, it will call the actual method.方法执行:按特定顺序执行所有before_filter方法后,它将调用实际方法。 All the data is available in params hash in this method.在此方法中,所有数据都可以在params散列中使用。 It processes input data, invokes Model calls for database access, and prepare data for view.它处理输入数据,调用模型调用以访问数据库,并准备数据以供查看。
  4. View: Proper view file will be chosen based on the controller#action, format.视图:将根据控制器#action、格式选择合适的视图文件。 Or you might select any particular view to render by render :partial call.或者您可以通过render :partial调用选择要呈现的任何特定视图。 And the response will be prepared using the variables prepared in controller.并且将使用控制器中准备的变量准备响应。 This response will go to the client.此响应将发送给客户端。
  5. After Filters: After processing the view, it will look after_filter methods and will those if found.过滤器之后:处理视图后,它将查看after_filter方法,如果找到,将查找after_filter方法。

Well this was a quick overview i would say, without really any details.嗯,这是一个快速概述,我想说,没有任何细节。 Im saying again, the pdf you referred really contains more details.我再说一遍,你提到的pdf确实包含更多细节。

Let me know if you want to know anything more specifically.如果您想了解更具体的内容,请告诉我。

A user opens his browser, types in a URL, and presses Enter.用户打开浏览器,输入 URL,然后按 Enter。 When a user presses Enter, the browser makes a request for that URL.当用户按下 Enter 键时,浏览器会请求该 URL。 The request hits the Rails router (config/routes.rb).请求命中 Rails 路由器 (config/routes.rb)。 The router maps the URL to the correct controller and action to handle the request.路由器将 URL 映射到正确的控制器和操作来处理请求。 The action receives the request, and asks the model to fetch data from the database.该动作接收请求,并要求模型从数据库中获取数据。 The model returns a list of data to the controller action.该模型将数据列表返回给控制器操作。 The controller action passes the data on to the view.控制器动作将数据传递给视图。 The view renders the page as HTML.该视图将页面呈现为 HTML。 The controller sends the HTML back to the browser.控制器将 HTML 发送回浏览器。 The page loads and the user sees it.页面加载并且用户看到它。

https://www.codecademy.com/articles/request-response-cycle-dynamic and https://www.codecademy.com/articles/request-response-cycle-forms https://www.codecademy.com/articles/request-response-cycle-dynamichttps://www.codecademy.com/articles/request-response-cycle-forms

Everything starts when 'url' requested by a user.当用户请求“url”时,一切都开始了。 The browser needs to know sever's IP address to connect, So it lookup DNS(Domain name system) which translate your domain into the public IP address of the particular server.浏览器需要知道服务器的 IP 地址才能连接,因此它会查找 DNS(域名系统),将您的域转换为特定服务器的公共 IP 地址。 Then the Browser will do threeway handshake to connect server like puma in port 80. And decide upon public and private key it happen only because if url use HTTPS.然后浏览器将进行三次握手以在端口 80 中连接像 puma 这样的服务器。并决定公钥和私钥,这只是因为 url 使用 HTTPS。 HTTPS is a secure wrapper around HTTP and TCP. HTTPS 是围绕 HTTP 和 TCP 的安全包装器。 Then Server triggers the rails application through middleware like rack and provides request verb, header, body to the application.然后 Server 通过像 rack 这样的中间件触发 rails 应用程序,并向应用程序提供请求动词、标题、正文。 Then rails application use Journey (Default route library of rails) to find the consent controller and action which matches the request and call with the request and params.然后 rails 应用程序使用 Journey(rails 的默认路由库)来查找与请求匹配的同意控制器和操作,并使用请求和参数进行调用。

Then rails lifecycle callbacks like before, after, around will be triggered during the process.然后在此过程中会触发像 before、after、around 之类的 rails 生命周期回调。 The action takes care of requesting data from the model and rendering the consent view for the request.该操作负责从模型请求数据并呈现请求的同意视图。 Finally sent back the status, header, and body as the response.最后将状态、标头和正文作为响应发回。

If you want to learn in-depth about lifecycle, check this article The Lifecycle of a Request如果您想深入了解生命周期,请查看这篇文章请求的生命周期

It is also important to note that Rails apps use an MVC architectural pattern , which is Model , View , and Controller at a high-level the life-cycle of a request in rails app is simply the interaction of the Model , View , and Controller .同样重要的是要注意 Rails 应用程序使用MVC architectural pattern ,即ModelViewController在高层次上 Rails 应用程序中请求的生命周期只是ModelViewController的交互. This article gives you an overview.这篇文章给你一个概述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM