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

Reasons why Ruby on Rails is the next BIG framework

At Prodispace we do development on Ruby on Rails. We have chosen this framework because we firmly believe that there is nothing better out there.

“Ruby on Rails is astounding. Using it is like watching a kung-fu movie, where a dozen bad-ass frameworks prepare to beat up the little newcomer only to be handed their asses in a variety of imaginative ways.”

-Nathan Torkington, O’Reilly Program Chair for OSCON

The elevator pitch:
Ruby on rails is an open source framework that’s optimised for programmer happiness and sustainable productivity. It lets you write beautiful code by favouring convention over configuration.

Five reasons to use Rails:

  1. Convention is favoured over configuration – This means putting everything in its correct place, using proper naming conventions etc. It also means:
    • Project teams work better because developers can understand what the other team members did
    • Source Code (Read intellectual property) is more maintainable and or sell-able.
    • Coders spend less time coding and more time developing because they don’t have to re-invent the wheel.
  2. Rails uses a MVC (Model View Controller) architecture. This allows developers to extract logic into Model Classes keeping Controllers skinny and View stupid i.e. keep all the logic of your application where it is maintainable and at its most powerful because you are as close as possible to the actual data.
  3. Test Driven development friendly obsessive architecture that helps you to manage change and improve quality
  4. An open-minded, helpful, motivated, excited community is continuously improving and expanding the technology
  5. Great version control of not only the core framework code, but also of code extensions called Gems (most often written and maintained by the community). Ruby and Rails and GIT – a source code versioning and management system – are synonymous. It is easily possible to sustain legacy software developed in older versions of ruby on rails indefinitely, something that is absolutely critical if you want to do business with large companies.
© Copyright - Designed by Pexeto