A Simple explanation of a MVC Ruby on Rails App

This post attempts to explain in layman’s terms how a Ruby on Rails Web application works. In keeping with Rails tradition I will try to illustrate the principles of MVC using a simple blog site application as an example

The Ruby on rails framework is based on MVC architecture:

  • M is for model
  • V is for View
  • C is for Controller

I will discuss each of these elements role in a web application, however, one should first look at the key ingredient of the web application: the Database. A database is simply a set of tables with rows of data indexed (identified) by a unique key value (such as a number).

The term relational database refers to that the different tables in the database reference each other and that there is some sort of data hierarchy in place.

A simple diagram showing how database table are constructed

In Rails, data in the table rows relate to objects – an object is a collection of data that is type specific.

An schematic illustrating what an object is

Objects are governed by models, which lay down the rules of how the database data should be interpreted and how data types (tables) relate to each other. Models can also define custom behaviour or methods for extraction information from objects.

Models define how data is interpreted and stored in the database

When a request comes in from the web, the request is interpreted and a command is sent to a controller. A controller is fetches data from the database and knows which view should be rendered, the object (or objects) needed are fetched from the database and passed on to the view.

controllers fetch data from the database to give to the views

Views are simple templates. By feeding rails’ rendering engine the view and the relevant data objects (provided by the controller). HTML (web browser mark-up language) is generated and sent back to the browser that made the request in the first place. The browser receives an HTML file and is none the wiser.

The schematic below attempts to illustrate the entire process.

A schematic showing how a MVC web application works

This is just a basic explanation of the process; there is a lot more detail to it. However, I hope this post has been able visualise the process of how a rails app works.

For more information refer to the excellent Rails Guides: Getting Started with Rails

© Copyright - Designed by Pexeto