ruby on rails_了解Ruby on Rails的基础:HTTP,MVC和路由

ruby on rails

After learning your first programming language, you may ask what can you do with programming: AI/Machine Learning? Hardware development? Mobile apps? Or maybe you want to start developing web applications! :)

学习完第一门编程语言后 ,您可能会问如何使用编程:AI /机器学习? 硬件开发? 移动应用? 或者,也许您想开始开发Web应用程序! :)

Here we’ll understand the basics of how the web, the routes, and the MVC architecture work using the Ruby on Rails web framework. Let’s dive into the web world.

在这里,我们将了解使用Ruby on Rails Web框架的Web,路由和MVC体系结构的基础知识。 让我们深入网络世界。

Before learning web development with Rails, I really recommend learning about Ruby first.

在学习使用Rails进行Web开发之前,我真的建议您先学习Ruby

网络如何运作? (How does the web work?)

The web has a bunch of layers (Application, TCP, Internet, Hardware layers) that are all connected. But basically, it works through HTTP (Hypertext Transfer Protocol).

Web具有连接在一起的一堆层( 应用程序,TCP,Internet,硬件层 )。 但基本上,它通过HTTP ( 超文本传输​​协议 )工作。

The Hypertext Transfer Protocol (HTTP) is an application protocol for distributed, collaborative, hypermedia information systems. — Wikipedia

超文本传输​​协议 ( HTTP )是用于分布式,协作式超媒体信息系统的应用程序协议。 — 维基百科

The HTTP works like a request — response cycle in the client — server model.

HTTP的工作方式类似于请求- 客户端中的 响应周期-服务器模型。

We have a web browser (Google Chrome, for example). So we type the www.google.com URL, and the client submits the HTTP request (request message) to the server. The server returns the HTTP response (response message — in that case, the response is the HTML from Google’s website).

我们有一个网络浏览器(例如Google Chrome)。 因此,我们输入www.google.com URL, 客户端HTTP 请求 (请求消息)提交到服务器服务器返回HTTP响应 (响应消息-在这种情况下,响应是Google网站HTML)。

The client does the request and receives the response from the server. The client handles the UI and user interactions. On the server, we can store and retrieve data (on databases), process logic on the background (workers/jobs), and a lot of other things.

客户端 发出请求并从服务器接收响应 。 客户端处理UI和用户交互。 在服务器上,我们可以存储和检索数据(在数据库上),在后台处理逻辑(工作人员/工作)以及许多其他事情。

If you want to deeply understand it, I’ll suggest some resources. I am a big fan of Preethi’s posts. Here a series of 3 parts:

如果您想深入了解它,我将建议一些资源。 我是Preethi帖子的忠实粉丝。 这里分为三个部分

  • A Primer for Newcomers to Web Development

    Web开发新手入门

  • Client-Server Model & the Structure of a Web Application

    客户端-服务器模型和Web应用程序的结构

  • HTTP & REST

    HTTP和REST

MVC架构和Rails路线 (The MVC architecture and Rails Routes)

Now that we understand how the Web works, we’ll study the MVC architecture and Rails Routes.

现在,我们了解了Web的工作原理,我们将研究MVC架构和Rails路由。

MVC stands for Model, View, and Controller.

MVC代表模型,视图和控制器。

On this architecture, we have the “separation of the concerns” among Models, Views and, Controllers. Each part has its own responsibility. Let’s dive into each part.

在这种架构上,我们在模型,视图控制器之间实现了“ 关注点分离 每个部分都有自己的责任。 让我们深入研究每个部分。

模型 (Model)
“Maintains the relationship between Object and Database and handles validation, association, transactions”
“维护对象和数据库之间的关系,并处理验证,关联,事务”

This means that the model will maintain an extreme relation with the Database. Each model (can) represent a database table (in case of SQL databases). This model object gains capabilities (inherited from ActiveRecord — Rails class) to retrieve, save, edit, and delete data from database table. We use model objects as a layer between our application and the database.

这意味着该模型将与数据库保持极端关系。 每个模型(可以)代表一个数据库表(在SQL数据库的情况下)。 该模型对象具有从数据库表中检索,保存,编辑和删除数据的功能(从ActiveRecord继承-Rails类)。 我们使用模型对象作为应用程序和数据库之间的一层。

Besides that relation with the database, the model can create validations and associations between models.

除了与数据库的关系之外, 模型还可以创建模型之间的验证关联

视图 (View)
“A presentation of data in a particular format, triggered by a controller’s decision to present the data.”
“以特定格式呈现数据,由控制器决定呈现数据触发。”

This is the presentation of the request’s response. This presentation can be in a bunch of format types: PDF, HTML, JSON, etc. The final result of a view will likely be the user interface (UI) — Part of the “Client.”

这是请求响应的表示 该演示文稿可以采用多种格式类型: PDF,HTML,JSON等。视图的最终结果可能是用户界面(UI),即“客户端”的一部分。

For most pages on the web, the views will be an HTML styled with CSS and JS. But we can implement PDFs of user behavior on a Travel digital product to show all employees how people use their website, too.

对于网络上的大多数页面,视图将是使用CSS和JS样式HTML。 但是,我们可以在Travel数字产品上实现用户行为的PDF,以向所有员工展示人们如何使用他们的网站。

控制者 (Controller)
“The facility within the application that directs traffic, on the one hand querying the models for specific data, and on the other hand organizing that data (searching, sorting) into a form that fits the needs of a given view.”
“应用程序中用于引导流量的工具,一方面查询模型中的特定数据,另一方面将数据组织(搜索,排序)为适合给定视图需求的形式。”

The controller is the “Maestro.” It takes care of the flow: uses models to do queries, parses data, and make decisions about in which format you’ll present the data.

控制器是“大师”。 它负责流程:使用模型进行查询,解析数据并决定将以哪种格式呈现数据。

在Rails应用程序上进行MVC和路由循环 (MVC & Routes cycle on a Rails application)

So imagine that we work at a Travel Startup. Part of the product is to present a list of great articles about travel stories and tips to travelers.

因此,想象一下我们在旅行启动公司工作 。 产品的一部分是向旅行者展示有关旅行故事和提示的出色文章列表 。

Just think from the traveler’s perspective. You go to www.worldpackers.com/articles and you see a beautiful page listing a bunch of great articles.

只是从旅行者的角度考虑。 您访问www.worldpackers.com/articles ,您会看到一个漂亮的页面,其中列出了许多很棒的文章。

When you type this URL in the browser, it makes a request to the server. In the server, we have the Rails web application. The Rails Router verifies if there is an entry matching the requested URL.

在浏览器中键入此URL时,它将向服务器发出请求。 在服务器中,我们有Rails Web应用程序。 Rails路由器会验证是否存在与请求的URL匹配的条目。

We just need to configure the routes for this line:

我们只需要为此行配置路由:

This will create RESTful routes for articles. If we run bundle exec rake routes, it will show the list of paths created.

这将为文章创建RESTful路由。 如果我们运行bundle exec rake routes ,它将显示创建的路径列表。

The HTTP verb can be GET, POST, PATCH, PUT, or DELETE. And we know how Rails maps each PATH to the right controller and action. Read more here.

HTTP动词可以是GETPOSTPATCHPUTDELETE 。 我们知道Rails如何将每个PATH映射到正确的controlleraction 。 在这里阅读更多。

In our case, the server will receive /articles path and GET as the HTTP verb. It will map to ArticlesController and index action.

在我们的例子中,服务器将接收/articles路径和GET作为HTTP动词。 它将映射到ArticlesControllerindex操作。

In the controller ArticlesController we use the model Article to get all articles in the database and render the view index.html.erb as the server response (the UI).

控制器 ArticlesController我们使用模型 Article来获取数据库中的所有文章,并将视图 index.html.erb呈现为服务器响应 (UI)。

By convention, this controller will render the view in views/articles/index.html.erb. Basically, it’s a plain HTML file powered by Ruby.

按照约定,此控制器将在views/articles/index.html.erb呈现视图。 基本上,这是由Ruby提供支持的纯HTML文件。

The Rails request-response cycle is one of the first concepts you need to understand when you start learning web development.

Rails请求-响应周期是开始学习Web开发时需要了解的第一个概念。

The user does stuff (request to the server), the the Rails application has the router to map the URL path to the right controller. In the controller, we can do all things with a model (or more than one model) — meaning getting, saving, editing, deleting data — and render a view to the user.

用户做东西(向服务器请求),Rails应用程序使路由器将URL路径映射到正确的控制器。 在控制器中,我们可以使用一个模型(或多个模型)来做所有事情-意味着获取,保存,编辑,删除数据-并向用户呈现视图。

就这样! (That’s all!)

We learned a lot here. I hope you guys appreciate the content and learn more about how the MVC architecture and routing work on Rails.

我们在这里学到了很多东西。 希望大家对此有所了解,并了解有关MVC架构和路由在Rails上如何工作的更多信息。

This is one more step forward in my journey to learning and mastering Rails and web development. You can see the documentation of my complete journey here on my Renaissance Developer publication.

这是我学习和掌握Rails和Web开发的又一个进步。 您可以在我的Renaissance Developer出版物上看到我完整旅程的文档。

If you want a complete Ruby and Rails course, learn real-world coding skills and build projects, try One Month Ruby Bootcamp and Rails Bootcamp. See you there ☺

如果您想学习一门完整的Ruby and Rails课程,学习现实世界的编码技巧并构建项目,请尝试一个月的Ruby Bootcamp Rails训练营 。 在那里见you

Have fun, and keep learning and coding.

玩得开心,继续学习和编码。

My Twitter & Github. ☺

我的Twitter和Github 。 ☺

翻译自: https://www.freecodecamp.org/news/understanding-the-basics-of-ruby-on-rails-http-mvc-and-routes-359b8d809c7a/

ruby on rails

你可能感兴趣的:(数据库,网络,python,java,大数据)