What is MVC in Ruby on Rails?

38,088

Solution 1

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!

Solution 2

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:

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.

Solution 3

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

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.

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.

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.

Solution 4

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

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.

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.

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.

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

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

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

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

Solution 5

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.

In Rails, the model is a data structure.

Share:
38,088
Imran
Author by

Imran

Web developer

Updated on July 09, 2022

Comments

  • Imran
    Imran almost 2 years

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

  • Theraot
    Theraot about 12 years
    It is always good to read about MVC from its creator, and then recognize that it doesn't really apply to web applications. You can read more at heim.ifi.uio.no/~trygver/2007/MVC_Originals.pdf
  • John Mikic
    John Mikic about 10 years
    This is a good general answer but not related to Ruby on Rails specifically. See this link for better answer: tutorialspoint.com/ruby-on-rails/rails-framework.htm
  • Serguei Fedorov
    Serguei Fedorov over 8 years
    One thing that annoys me about these charts is that it doesn't really do justice in explaining how the model is passed between the view and the controller. It makes it seem as though the model is a separate entity, but it's actually the data package passed between the view and controller. The controller technically controls what happens on to the model on the server; in this case loading data from the database. The chart makes it seem like the model is in charge of this (has logic to do so), when really the controller makes this call.
  • Martin Tournoij
    Martin Tournoij about 8 years
    Ruby on Rails doesn't have any real separation between the "View" and the "Controller". Views are executed in the context of a controller and effectively the same as a controller code (except with a different syntax). This is why @var always works, and is markedly different from many other frameworks, where Views are executed completely separate from Controllers.