Ruby on Rails MVC Framework - W3Schools

A framework can be defined as a set of programs with built-in libraries which help you in writing most of your web application’s code. When a developer uses a framework to build a web application, the main objective is to save time and build a task specific application.

Now, when you are using Rails for developing an application after ignoring the configuration and other housekeeping chores, there are 3 major tasks that you are mandatorily required to perform, which are –

  1. Describing and Modelling the Domain of Your Rails Application

The described domain will act like the universe to your web application. It could be an address book, a hardware inventory, a play store, or a varsity. So, it is important that you decide the entities of this universe beforehand. Make sure that all the items of this universe should have a relatable connection with each other. The simple example is the inter-linked nature of the entities of a database.

  1. Specification of the Actions Under the Domain

The model of the domain is static by default, but you can make it dynamic by working on it. For example, you can add as many addresses you want in an address book. There will be a purchasing department in the play store. Likewise, the hardware shop too will have a purchasing department. And in a university, students can easily enroll them for various classes. For this to work efficiently, you really need to look for all the possible actions or scenarios which can appear in front of the elements of your domain in a dynamic environment.

  1. Choosing and Designing of Views of the Domain Available Publicly

At this last stage, you can start thinking in terms of your domain’s appearance on the web browser. For an instance, if you are having a university domain then you need to have a welcome page, registration page, a confirmation page, a page dedicated to all the information related to different classes, and various others. All of these web pages will lead the user to a specific progression in the whole process.

The above three tasks make Ruby on Rails to deal with the Model-View-Controller (MVC) framework.

Ruby on Rails MVC Framework

The primary principle of MVC is to divide the web application into three individual subsystems which will co-operate with each other later in the process.

Model – ActiveRecord
This subsystem is responsible for the relationship between several objects and the database. It takes care of validations, associations, transactions, and many other operations.

The Model subsystem always gets implemented under the ActiveRecord library. This library offers a user interface along with the binding capability to the tables in a relational database. This is done with the help of Ruby code which is capable of modifying the records of the database. The method names in Ruby gets generated automatically depending upon the field names used in the tables of the database.

View – ActionView
A view can be referred to as the presentation of data in a specific format. The representation of the data is directly associated with the decision of the controller. The script-based template systems (such as JSP, ASP, PHP, etc.) are used. The integration of these systems with AJAX technology is very easy.

This subsystem gets implemented under the ActionView library. The ActionView library is an Embedded Ruby (ERb) based system. The subsystem is used to define the presentation templates for the representation of the data. Whenever a web connects to a Rails web application, it always results in a particularly defined display of the view.

Controller – ActionController
The web application built under the Rails framework is capable of directing traffic. It can pass a query for the specific type of data and at the same time, organize this data (performing operations such as searching, sorting, and messaging) into the specified form of the view.

This subsystem gets implemented under the ActionController library. It acts as a data broker between the ActiveRecord (which is a database interface) and the ActionView (which is a presentation engine).

Simplified Representation of MVC Framework
The below provided pictorial representation will help you to understand the Ruby on Rails framework from closely –

Directory Based Representation of MVC Framework
Here, we will be considering that the installation has been taken place on Linux, then the directory-based representation of MVC framework will appear as the following –

tp> cd /usr/local/lib/ruby/gems/2.2.0/gems
tp> ls

Under this, you will see various sub-directories which may or may not exceed the given list –

  • actionpack-x.y.z
  • activerecord-x.y.z
  • rails-x.y.z

In case, you have installed your Rails framework on Windows then the directory representation will be like –

tp>cd ruby\lib\ruby\gems\2.2.0\gems

Under this, you will see various sub-directories which may or may not exceed as shown in the below provided screenshot –

In here, you can clearly see that ActionView and ActionController are bundled together under the ActionPack.

On one hand, ActiveRecord offers a set of programming techniques and shortcuts to modify or manipulate the data present under an SQL database. On the other hand, ActionController and ActionView are responsible for manipulating and representing the data, respectively. And Ruby on Rails is a framework which binds all of these subsystems under one roof.