ruby on rails_Ruby on Rails应用程序流程

柳和怡
2023-12-01

ruby on rails

Rails申请流程 ( Rails Application Flow )

When you're writing your own programs from beginning to end, it's easy to see flow control. The program starts here, there's a loop there, method calls are here, it's all visible. But in a Rails application, things are not so simple. With a framework of any kind, you relinquish control of such things as "flow" in favor of a faster or simpler way to do complex tasks. In the case of Ruby on Rails, the flow control is all handled behind the scenes, and all you're left with is (more or less) a collection of models, view and controllers.

从头到尾编写自己的程序时,很容易看到流控制 。 程序从这里开始,那里有一个循环,这里有方法调用,都可见。 但是在Rails应用程序中,事情并不是那么简单。 使用任何类型的框架,您都将放弃对“流”之类的控制,而希望以更快或更简单的方式来完成复杂的任务。 对于Ruby on Rails,流控制全部在后台处理,剩下的(或多或少)是模型,视图和控制器的集合。

HTTP ( HTTP )

At the core of any web application is HTTP. HTTP is the network protocol your web browser uses to talk to a web server. This is where terms like "request," "GET" and "POST" come from, they're the basic vocabulary of this protocol. However, since Rails is an abstraction of this, we won't spend much time talking about it.

HTTP是任何Web应用程序的核心。 HTTP是Web浏览器用来与Web服务器通信的网络协议。 这是诸如“ request”,“ GET”和“ POST”之类的术语的来源,它们是该协议的基本词汇。 但是,由于Rails是对此的抽象,因此我们不会花太多时间谈论它。

When you open a web page, click on a link or submit a form in a web browser, the browser will connect to a web server via TCP/IP. The browser then sends the server a "request," think of it like a mail-in form that the browser fills out asking for information on a certain page. The server ultimately sends the web browser a "response." Ruby on Rails is not the web server though, the web server can be anything from Webrick (what usually happens when you start a Rails server from the command line) to Apache HTTPD (the web server that powers most of the web). The web server is just a facilitator, it takes the request and hands it to your Rails application, which generates the response and passes is back to the server, which in turn sends it back to the client. So the flow so far is:

当您打开网页,单击链接或在Web浏览器中提交表单时,浏览器将通过TCP / IP连接到Web服务器。 然后,浏览器向服务器发送“请求”,将其视为一种邮寄表格,浏览器填写该表格以在特定页面上询问信息。 服务器最终向Web浏览器发送“响应”。 Ruby on Rails并不是Web服务器,Web服务器可以是从Webrick(通常是从命令行启动Rails服务器时发生的情况)到Apache HTTPD(为大多数Web供电的Web服务器)的任何东西。 Web服务器只是一个促进者,它接收请求并将其交给您的Rails应用程序,后者会生成响应并将其传递回服务器,然后服务器又将其发送回客户端。 因此,到目前为止的流程是:

Client -> Server -> [Rails] -> Server -> Client
客户端->服务器-> [Rails]->服务器->客户端

But "Rails" is what we're really interested in, let's dig deeper there.

但是“ Rails”是我们真正感兴趣的,让我们在此进行更深入的研究。

路由器 ( The Router )

One of the first thing a Rails application does with a request is to send it through the router. Every request has a URL, this is what appears in the address bar of a web browser. The router is what determines what is to be done with that URL, if the URL makes sense and if the URL contains any parameters. The router is configured in config/routes.rb.

Rails应用程序处理请求的第一件事就是通过路由器发送请求。 每个请求都有一个URL,这就是Web浏览器地址栏中显示的URL。 路由器决定了该URL的处理方式,该URL是否有意义以及该URL是否包含任何参数。 路由器在config / routes.rb配置

First, know that the ultimate goal of the router is to match a URL with a controller and action (more on these later). And since most Rails applications are RESTful, and things in RESTful applications are represented using resources, you'll see lines like resources :posts in typical Rails applications. This matches URLs like /posts/7/edit with the Posts controller, the edit action on the Post with the ID of 7. The router just decides where requests go. So our [Rails] block can be expanded a bit.

首先,要知道路由器的最终目标是将URL与控制器和动作进行匹配(稍后会详细介绍)。 而且,由于大多数Rails应用程序都是RESTful的,并且RESTful应用程序中的事物都是使用资源表示的,因此您会在典型的Rails应用程序中看到诸如resource:posts之类的行。 这将使用Posts控制器匹配/ posts / 7 / edit之类的URL,对ID为7的Post进行edit操作。路由器仅决定请求的去向。 因此我们的[Rails]块可以扩展一点。

Router -> [Rails]
路由器-> [路轨]

  (   )

控制器 ( The Controller )

Now that the router has decided which controller to send the request to, and to which action on that controller, it sends it on. A Controller is a group of related actions all bundled together in a class. For instance, in a blog, all of the code to view, create, update and delete blog posts is bundled together in a controller called "Post." The actions are just normal methods of this class. Controllers are located in app/controllers.

现在,路由器已决定将请求发送到哪个控制器,以及对该控制器执行的操作,它将继续发送请求。 控制器是一组相关的动作,所有这些动作捆绑在一个类中。 例如,在博客中,用于查看,创建,更新和删除博客文章的所有代码都捆绑在一个称为“帖子”的控制器中。 这些动作只是此类的常规方法 。 控制器位于app / controllers中

So let's say the web browser sent a request for /posts/42. The router decides this refers to the Post controller, the show method and the ID of the post to show is 42, so it calls the show method with this parameter. The show method is not responsible for using the model to retrieve the data and using the view to create the output. So our expanded [Rails] block is now:

假设网络浏览器发送了/ posts / 42的请求。 路由器将其确定为引用Post控制器, show方法以及要显示的帖子的ID为42 ,因此它将使用此参数调用show方法。 show方法不负责使用模型检索数据以及使用视图创建输出。 因此,我们扩展后的[Rails]块现在是:

Router -> Controller#action
路由器->控制器#动作

该模型 ( The Model )

The model is both the simplest to understand and most difficult to implement. The Model is responsible for interacting with the database. The simplest way to explain it is the model is a simple set of method calls that return plain Ruby objects that handle all interactions (reads and writes) from the database. So following the blog example, the API the controller will use to retrieve data using the model will look something like Post.find(params[:id]). The params is what the router parsed from the URL, Post is the model. This makes SQL queries, or does whatever is needed to retrieve the blog post. Models are located in app/models.

该模型最容易理解,最难实现。 该模型负责与数据库进行交互。 解释模型的最简单方法是模型,该方法是一组简单的方法调用,这些方法调用返回处理数据库中所有交互(读取和写入)的普通Ruby对象。 因此,在博客示例之后,控制器将使用该模型使用API​​检索数据的API类似于Post.find(params [:id])参数是路由器从URL解析的内容,Post是模型。 这可以进行SQL查询,或执行检索博客文章所需的一切。 模型位于app / models中

It's important to note that not all actions need to use a model. Interacting with the model is only required when data needs to be loaded from the database or saved to the database. As such, we'll put a question mark after it in our little flowchart.

重要的是要注意,并非所有动作都需要使用模型。 仅当需要从数据库加载数据或将数据保存到数据库时才需要与模型进行交互。 这样,我们将在小流程图中添加一个问号。

Router -> Controller#action -> Model?
路由器-> Controller#action->模型?

风景 ( The View )

Finally, it's time to start generating some HTML. HTML is not handled by the controller itself, nor is it handled by the model. The point of using an MVC framework is to compartmentalize everything. Database operations stay in the mode, HTML generation stays in the view, and the controller (called by the router) calls them both.

最后,是时候开始生成一些HTML了。 HTML既不由控制器本身处理,也不由模型处理。 使用MVC框架的目的是将所有内容分隔开。 数据库操作停留在该模式,HTML生成停留在视图,并且控制器(由路由器调用)将它们都调用。

HTML is normally generated using embedded Ruby. If you're familiar with PHP, that is to say an HTML file with PHP code embedded in it, then embedded Ruby will be very familiar. These views are located in app/views, and a controller will call one of them to generate the output and send it back to the web server. Any data retrieved by the controller using the model will generally be stored in an instance variable which, thanks to some Ruby magic, will be available as instance variables from within the view. Also, embedded Ruby doesn't need to generate HTML, it can generate any type of text. You'll see this when generating XML for RSS, JSON, etc.

HTML通常是使用嵌入式Ruby生成的。 如果您熟悉PHP,也就是说,其中嵌入了PHP代码HTML文件,那么嵌入式Ruby将非常熟悉。 这些视图位于app / views中 ,控制器将调用其中的一个以生成输出并将其发送回Web服务器。 控制器使用模型检索的任何数据通常都将存储在一个实例变量中,这要归功于某些Ruby魔术,可以在视图中作为实例变量使用。 另外,嵌入式Ruby不需要生成HTML,它可以生成任何类型的文本。 在为RSS,JSON等生成XML时,您会看到此信息。

This output is sent back to the web server, which sends it back to the web browser, which completes the process.

此输出发送回Web服务器,然后将其发送回Web浏览器,以完成该过程。

完整图片 ( The Complete Picture )

And that's it, here is the complete life of a request to a Ruby on Rails web application.

就是这样,这就是对Ruby on Rails Web应用程序的请求的完整寿命。

  1. Web Browser - The browser makes the request, usually on behalf of the user when they click on a link.

    Web浏览器 -浏览器通常在用户单击链接时代表用户发出请求。

  2. Web Server - The web server takes the request and sends it to the Rails application.

    Web服务器-Web服务器接收请求并将其发送到Rails应用程序。
  3. Router - The router, the first part of the Rails application that sees the request, parses the request and determines which controller/action pair it should call.

    路由器-路由器,Rails应用程序的第一部分,看到请求,解析请求并确定应调用的控制器/操作对。
  4. Controller - The controller is called. The controller's job is to retrieve data using the model and send it to a view.

    控制器-调用控制器。 控制器的工作是使用模型检索数据并将其发送到视图。
  5. Model - If any data needs to be retrieved, the model is used to get data from the database.

    模型-如果需要检索任何数据,则使用模型从数据库中获取数据。
  6. View - The data is sent to a view, where HTML output is generated.

    视图-数据发送到一个视图,在该视图中生成HTML输出。
  7. Web Server - The generated HTML is sent back to the server, Rails is now finished with the request.

    Web服务器 -生成HTML发送回服务器,Rails现在完成了请求。

  8. Web Browser - The server sends the data back to the web browser, and the results are displayed.

    Web浏览器-服务器将数据发送回Web浏览器,并显示结果。

翻译自: https://www.thoughtco.com/rails-application-flow-2908211

ruby on rails

 类似资料: