27

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

0

8 Answers 8

50

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!

3
  • 1
    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
    – Theraot
    Mar 14, 2012 at 7:48
  • 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
    – John Mikic
    Mar 21, 2014 at 15:31
  • 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. Mar 6, 2016 at 17:27
5

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.

1
  • 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. Jan 21, 2016 at 1:18
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.

2

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/

0
1

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

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

1

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.

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.

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

1

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.

0

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

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.

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.

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.