什么是Ruby on Rails中的MVC? [英] What is MVC in Ruby on Rails?

查看:161
本文介绍了什么是Ruby on Rails中的MVC?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以用外行人士的方式在Ruby on Rails中向我解释MVC。我特别感兴趣的是理解MVC中的模型(不能让我的头部围绕模型)。

解决方案

MVC是(复合)设计模式,1979年由Trygve Reenskaug(Smalltalk)开发。



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



模型




  • 包含应用程序的数据(通常链接到数据库) li>
  • 包含应用程序的状态(例如客户的订单)

  • 包含所有业务逻辑

  • 通知查看状态更改(**不适用于ROR,请参见下文)

  • 不了解用户界面,因此可以重复使用



查看




  • 生成向用户显示数据的用户界面

  • 被动,即不进行任何处理

  • 一旦数据显示给用户,视图工作就会完成。

  • 视图可以访问相同的模型,因为不同的原因



控制器




  • 从外界接收事件, d(通常通过视图)

  • 与模型交互

  • 向用户显示适当的视图



**经典MVC不适合于Web应用程序,因为模型不能以观察者的方式发送所有对视图的更改(视图是一个网页)。引入Model2来克服JSP团队在90年代改变基础设施。 MVC Web框架真的不是MVC,但是Model2(Ruby on Rails是这样的)。



这是一个GUI模式的描述,包括来自主人的MVC,Martin Fowler
GUI架构



我迄今为止发现的最好的书是敏捷网页使用Rails开发。它开始于假设没有知识,而且是相当全面的。



希望这有助于为您带来一些光明!


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).

解决方案

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

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

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

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).

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

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

Hope this helps to shed some light for you!

这篇关于什么是Ruby on Rails中的MVC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆