You are here
Home > howto >

MVC architecture in Ruby On Rails Quick Guide

When we are beginner to Ruby on rails , or just getting started , we come across the term MVC . MVC stands for Model , View and Controller . But there are other components involved also . Following image shows how all the components of MVC relate to each other . Now lets have a closer look to each of them .

Following are the core components that we must be familiar with .

  1. Model
  2. View
  3. Controller
  4. Routing System
  5. Database

Before diving deep into the Rails MVC world , let me give you a bird eye view of whats happening here .

First of all , let us assume that you are the user of this application say AppX !

Now , when you open the site first time , you made a request to browser to load the page . On behalf of you , browser sent a request to appropriate server . Now this server triggers rails routing system and passes URL to it . Now from available information from URL , rails routing system matches it with patterns available to it . If some pattern matches to requested URL , then corresponding controller is triggered by routing system . Btw these patterns are called routes.

Now , controller is triggered . According to functionality required , controller requests data to model , model makes appropriate query to database and returns data to controller . The data loaded in controller is also accessible to respective view file . This view file works as a template which in turn generates web page with all the requested data elements in it . This page is now sent to browser and user can enjoy or work with it !

Now that we have a general idea of whats happening here , we can go a level deeper


Model is one of the core components of MVC architecture.

Model deals with all the data and database related stuff . Like fetching data from DB , saving , updating and deleting records , creating new records . In short , it works with CRUD operations . Not only just CRUD , it is also important when it comes to data validation.

Model is a Ruby Class that is inherited from ActiveRecord::Base . A basic model looks like this

class ApplicationRecord < ActiveRecord::Base
   self.abstract_class = true

A very important point to notice is , the only component that interacts with database is Model . Means no other component can directly interact with DB which makes rails applications extremely secextremely secureure when models are implemented properly .


View is second core component of MVC architecture as well as Rails application.

View is nothing but a template file . There are templates like .html.erb , .html.haml etc . But in general , view file contains HTML code along with ruby code .
Just like javascript , where we use script tags to embed js code in html , in view files , we use <% %> and <%= %> to embed ruby code insidde HTML .

<% _ruby_code_here_ %> tag is used where we want to execute code but don’t want to show to user .

And <%= _ruby_code_here_ %.> tag is used when we want to execute and show results to user .

Another important thing is that , Views and controllers are closely related .

In any rails application , for each controller , there is a folder in Views folder of application . And for each controller method , there is a view .

notice that controllers and views are related

From above image we can see that , articles_controller.rb is related to articles directory in views directory . and same is true for comments_controller.rb.


This is third core component of a rails application.

Controllers mostly work as a middle man between database and a view. It is the program where most logic of program is written . Controller triggers other components .

A controller is nothing but a ruby class like model . But unlike model , it is inherited from ApplicationController class .

A controller contains multiple public and private methods . From these methods , only public ones are called Actions . For each Action , there is a Associated View .

Controller contains complete logic of application mainly the CRUD part of it .

A general controller will look like this .

class ArticlesController < ApplicationController
  def index
  def show
  def new
  def create
    redirect_to @article
    render :new
  def edit
  def update
   if @article.update(article_params)
    redirect_to @article
    render :edit
  def destroy
   redirect_to root_path
   def article_params

as you can see there are lots of methods . But if you notice their names , these are components of CRUD functionality . Create , Read, Update and Delete .

4.Routing System

Routing is not includes in MVC but it is the core component of a rails application . Without routing , we cannot imagine a rails app.

The main task of routing system is to trigger the appropriate controller action based on URL . i.e. if URL is then routing system will trigger the index action of blog controller .

All the possible routes for a rails application are located in app/config/routes.rb

routes.rb file located in app/config

A typical routes.rb file looks like this .


Database is crucial part of any rails application . We can create rails apps without a database but it will be a simple app.

Any enterprise grade real world application will require a strong database .

Rails support MySql , Sqlite3 and Postgresql by default . We can also use other databases but have to add additional gems .

Rails Database support is so mature that a developer only has to setup it once and we can just work on ruby code without even thinking about it once .

All the database management is done automatically by rails ,by models to be specific .

In ruby , all the components are treated as objects and so do the databases. Every model is associated with a database table . And all the database queries are carried out as it they are functions . i.e. tables are treated as object and all the DB functions are treated as object functions.

If Blog is a model we can just fetch all the data from Blogs table by writting , Blogs.all

Blog --> model
blogs -->table in database
Blog.all --> calling methods to fetch data from table .


After knowing basics about MVC and general working of a Rails Application , we can say that its a unique and efficient way of designing and developing webapplications . Where users can get what they request , database is separated from other components and there is a easy way to manipulate database too .

Leave a Reply