Ruby on Rails 中的 MVC 是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1931335/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:03:27  来源:igfitidea点击:

What is MVC in Ruby on Rails?

ruby-on-railsmodel-view-controllerdesign-patternsarchitecture

提问by Imran

Could someone please explain MVC to me in Ruby on Rails, in layman terms. I am especially interested in understanding the Model in MVC (can't get my head around the model).

有人可以在 Ruby on Rails 中用外行术语向我解释 MVC。我对理解 MVC 中的模型特别感兴趣(无法理解模型)。

回答by MGrev

Some background, MVC is a (compound) design pattern and was developed in 1979 by Trygve Reenskaug (Smalltalk).

在某些背景下,MVC 是一种(复合)设计模式,由 Trygve Reenskaug (Smalltalk) 于 1979 年开发。

True MVC was primarily planned for use in n-tier (non web) systems and it splits a system into 3 distinct parts, a Model, View and Controller

真正的 MVC 主要计划用于 n 层(非 Web)系统,它将系统分为 3 个不同的部分,模型、视图和控制器

The Model

该模型

  • Contains data for the application (often linked to a database)
  • Contains state of the application (e.g. what orders a customer has)
  • Contains all business logic
  • Notifies the View of state changes (** not true of ROR, see below)
  • No knowledge of user interfaces, so it can be reused
  • 包含应用程序的数据(通常链接到数据库)
  • 包含应用程序的状态(例如客户有什么订单)
  • 包含所有业务逻辑
  • 通知视图状态更改(** 不适用于 ROR,见下文)
  • 不了解用户界面,因此可以重复使用

The View

风景

  • Generates the user interface which presents data to the user
  • Passive, i.e. doesn't do any processing
  • Views work is done once the data is displayed to the user.
  • Many views can access the same model for different reasons
  • 生成向用户呈现数据的用户界面
  • 被动,即不做任何处理
  • 一旦数据显示给用户,视图工作就完成了。
  • 由于不同的原因,许多视图可以访问同一个模型

The Controller

控制器

  • Receive events from the outside world (usually through views)
  • Interact with the model
  • Displays the appropriate view to the user
  • 从外部世界接收事件(通常通过视图)
  • 与模型互动
  • 向用户显示适当的视图

** Classic MVC is not suited to web applications, as the model cannot send all changes to the view in an observer fashion (the view is a web page). The Model2 was introduced to overcome the changing infrastructure by JSP team in 90s . MVC Web frameworks are really not MVC, but Model2 (this is true of Ruby on Rails).

** 经典 MVC 不适合 Web 应用程序,因为模型不能以观察者的方式将所有更改发送到视图(视图是网页)。Model2 是为了克服 90 年代 JSP 团队不断变化的基础架构而引入的。MVC Web 框架实际上不是 MVC,而是 Model2(Ruby on Rails 也是如此)。

Here is a description of GUI patterns including MVC from the master, Martin Fowler GUI Architectures

这里是 GUI 模式的描述,包括来自大师 Martin Fowler GUI Architectures 的MVC

The best book I have found so far is Agile Web Development with Rails. It begins by assuming no knowledge, and is quite comprehensive.

迄今为止我找到的最好的书是Agile Web Development with Rails。它从假设没有知识开始,并且非常全面。

Hope this helps to shed some light for you!

希望这有助于为您提供一些启发!

回答by Anjan

enter image description here

在此处输入图片说明

MVC basically indicates Model-View-Controller. And MVC used by many languages like PHP, Perl, Python etc. Generally MVC works like this:

MVC 基本上表示模型-视图-控制器。MVC 被许多语言使用,如 PHP、Perl、Python 等。一般 MVC 的工作原理是这样的:

Request first comes to the controller, controller finds and appropriate view and interacts with model, model interacts with your database and send the response to controller then controller based on the response give the output parameter to view.

请求首先到达控制器,控制器找到合适的视图并与模型交互,模型与您的数据库交互并将响应发送给控制器,然后控制器根据响应给出输出参数以供查看。

回答by Bill K

Your Model is the data structure that your program uses.

您的模型是您的程序使用的数据结构。

The View is the part that interacts with the screen or the next level up.

视图是与屏幕或上一层交互的部分。

The Controller generally processes data between the model and view

Controller一般处理模型和视图之间的数据

MVC structures are often nested, so a "Model" or "View" may contain its own MVC (Think of a component on the screen. You may just fill it with a string, but behind the scenes the code of the component draws its own little view, has it's own little model (the string you pass in) and has a little controller drawing the data onto the view.

MVC 结构通常是嵌套的,因此“模型”或“视图”可能包含自己的 MVC(想想屏幕上的组件。您可能只是用字符串填充它,但在幕后,组件的代码绘制了自己的小视图,有它自己的小模型(您传入的字符串),并有一个小控制器将数据绘制到视图上。

In Rails, the roles of the model, view and controller are well-defined by the framework, any tutorial will point out the three components as it walks you through the files it created.

在 Rails 中,模型、视图和控制器的角色由框架明确定义,任何教程都会在引导您浏览它创建的文件时指出这三个组件。

In other systems, those pieces may be harder to identify. Also, MVC is not "Perfect", just keep in mind that there are valid alternatives, but it's a good way to start organizing.

在其他系统中,这些部分可能更难识别。此外,MVC 不是“完美”的,只要记住有有效的替代方案,但它是开始组织的好方法。

回答by Adam

MVC isn't specifically just for Ruby on Rails. It was actually created awhile before Ruby on Rails ever came around. It's mainly just a way of organizing your code so that you have some code that's responsible for your models (the Class version of a database table), some code that's responsible for your views (what's visually being displayed to the user) and code that's responsible for your controllers (what ties the views to the models and performs the bulk of your logic.

MVC 不仅仅适用于 Ruby on Rails。它实际上是在 Ruby on Rails 出现之前创建的。它主要只是组织代码的一种方式,以便您拥有一些负责模型的代码(数据库表的 Class 版本),一些负责您的视图的代码(视觉上向用户显示的内容)和负责的代码对于您的控制器(将视图与模型联系起来并执行大部分逻辑的东西。

That's the non-framework-specific description. Each framework that uses MVC has a different way of implementing it. For Ruby on Rails each model represents a database table as a class that can communicate directly in code with other objects without needing to write any SQL. All the SQL is being taken care of in the background and you just have to think of it as though it were a normal class (well almost, it's not seamless yet). The view is mostly HTML and represents what will be sent to the browser. The controller is just the code that communicates the models and views together.

那是非特定于框架的描述。每个使用 MVC 的框架都有不同的实现方式。对于 Ruby on Rails,每个模型都将数据库表表示为一个类,该类可以在代码中直接与其他对象通信,而无需编写任何 SQL。所有 SQL 都在后台处理,您只需要将它视为普通类(几乎,它还不是无缝的)。视图主要是 HTML,表示将发送到浏览器的内容。控制器只是将模型和视图通信在一起的代码。

All in all, MVC isn't specific just to Ruby on Rails...that's just the most popular.

总而言之,MVC 不仅仅针对 Ruby on Rails ……那只是最流行的。

回答by Pete

I think the best way to wrap your head around MVC is by example. Try coding up a simple Rails app using MVC. There are many tutorials online, such as the blog example at "Getting Started with Rails".

我认为了解 MVC 的最佳方式是举例。尝试使用 MVC 编写一个简单的 Rails 应用程序。网上有很多教程,例如“Rails 入门”中的博客示例

If chose to learn by coding an example, check out the answers to Where can I find clear examples of MVC?

如果选择通过编码示例来学习,请查看我在哪里可以找到清晰的 MVC 示例?

回答by Joey Guerra

Ruby on Rails does not implement the MVC design pattern. Ruby on Rails has folders called controllers, models, and views. The views folder has HTML files. The controllers and models folder have ruby files. The controllers map to a URL and a method in the controller gets executed upon requesting that URL, the associated view (HTML file) gets loaded and the model (data structure) for it is used to populate the view. Thats the extent of it's likeness to the MVC design pattern. It's a shame that people say it's MVC because it's caused a generation of confusion and misunderstanding of the MVC design pattern.

Ruby on Rails 没有实现 MVC 设计模式。Ruby on Rails 有称为控制器、模型和视图的文件夹。视图文件夹包含 HTML 文件。控制器和模型文件夹有 ruby​​ 文件。控制器映射到一个 URL,控制器中的一个方法在请求该 URL 时被执行,关联的视图(HTML 文件)被加载,它的模型(数据结构)用于填充视图。这就是它与 MVC 设计模式的相似程度。人们说它是 MVC 是一种耻辱,因为它引起了一代人对 MVC 设计模式的混淆和误解。

In Rails, the model is a data structure.

在 Rails 中,模型是一种数据结构。

回答by vinoth

The Model View Controller principle divides the work of an application into 3 separate but closely cooperative subsystems.

模型视图控制器原则将应用程序的工作划分为 3 个独立但紧密协作的子系统。

Model (ActiveRecord ):

模型(活动记录):

It maintains the relationship between the objects and the database and handles validation, association, transactions, and more.

它维护对象和数据库之间的关系并处理验证、关联、事务等。

This subsystem is implemented in ActiveRecord library, which provides an interface and binding between the tables in a relational database and the Ruby program code that manipulates database records. Ruby method names are automatically generated from the field names of database tables.

这个子系统是在 ActiveRecord 库中实现的,它在关系数据库中的表和操作数据库记录的 Ruby 程序代码之间提供了一个接口和绑定。Ruby 方法名称是根据数据库表的字段名称自动生成的。

View ( ActionView ):

视图(动作视图):

It is a presentation of data in a particular format, triggered by a controller's decision to present the data. They are script-based template systems like JSP, ASP, PHP, and very easy to integrate with AJAX technology.

它是以特定格式呈现数据,由控制者决定呈现数据触发。它们是基于脚本的模板系统,如 JSP、ASP、PHP,并且非常容易与 AJAX 技术集成。

This subsystem is implemented in ActionView library, which is an Embedded Ruby (ERb) based system for defining presentation templates for data presentation. Every Web connection to a Rails application results in the displaying of a view.

该子系统是在 ActionView 库中实现的,ActionView 库是一个基于嵌入式 Ruby (ERb) 的系统,用于定义数据表示的表示模板。与 Rails 应用程序的每个 Web 连接都会导致显示一个视图。

Controller ( ActionController ):

控制器(动作控制器):

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, messaging it) into a form that fits the needs of a given view.

应用程序中的设施,一方面,查询特定数据的模型,另一方面,将数据组织(搜索、排序、发送消息)到适合给定视图需求的形式中。

This subsystem is implemented in ActionController, which is a data broker sitting between ActiveRecord (the database interface) and ActionView (the presentation engine).

该子系统在 ActionController 中实现,ActionController 是位于 ActiveRecord(数据库接口)和 ActionView(呈现引擎)之间的数据代理。

Check the links below for clear understanding of mvc in rails:

检查以下链接以清楚了解 rails 中的 mvc:

http://www.bogotobogo.com/RubyOnRails/RubyOnRails_Model_View_Controller_MVC.php

http://www.bogotobogo.com/RubyOnRails/RubyOnRails_Model_View_Controller_MVC.php

https://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/

https://betterexplained.com/articles/intermediate-rails-understanding-models-views-and-controllers/

回答by Tony Tai Nguyen

Here's a brief overview at a high level on how the MVC Pattern works:

下面简要概述了 MVC 模式的工作原理:

Controller:

控制器:

  1. Listens on some kind of interaction/event stream.
  2. Controller can send the model that type of interaction/event.
  3. Controller can also communicate with the the view.
  1. 侦听某种交互/事件流。
  2. 控制器可以向模型发送那种类型的交互/事件。
  3. 控制器也可以与视图通信。

Model:

模型:

  1. Models will listen in on the interaction/event from the controller.
  2. Is an abstraction of a data source.
  3. Handles data logic and manipulation.
  4. After it is done with logic, it then sends to controller which will then communicate with the view.
  1. 模型将监听来自控制器的交互/事件。
  2. 是数据源的抽象。
  3. 处理数据逻辑和操作。
  4. 完成逻辑后,它会发送到控制器,然后控制器将与视图进行通信。

View:

看法:

  1. View can communicate with the controller.
  2. Knows how to render data from the Model to the browser visually.
  3. The Controller tells to View to do something with something from the Model.
  1. 视图可以与控制器通信。
  2. 知道如何将数据从模型可视化地呈现到浏览器。
  3. Controller 告诉 View 用 Model 中的东西做一些事情。


A couple of things to note is that models can't communicate with views directly and vise versa. Only the controller can communicate with the view and model, so the controller acts as the delegator for the interaction/event retrieved from users interaction on the browser.

需要注意的几件事是模型不能直接与视图通信,反之亦然。只有控制器可以与视图和模型进行通信,因此控制器充当从浏览器上的用户交互中检索的交互/事件的委托者。

check this link for more clear understanding

检查此链接以获得更清晰的理解

one more link to get clear

再一个链接搞清楚