Thursday, December 23, 2010

RoR-Understanding Model-view-controller

ROR(Ruby on Rails) uses the Model-View-Controller(MVC) Architecture.So as a beginner one should understand  MVC before getting into Ruby on Rails.
Before understanding MVC lets differentiate between traditional application and MVC application

Traditional application

  • Browser directly accesses Page

MVC application 

  • Browser directly accesses Controller

What is MVC
  • MVC is an Architectural Design pattern
  • MVC separates the web application into three different modules
To better understand MVC architecture let take a simple example 

Your brother or sister is having birthday tomorrow.So you decided to buy a cake and visit a Bakery Shop.You request the baker to make a wonderful cake and the Baker collects all the ingredients to make the cake.After collecting all the ingredients the baker decides on how the cake should look like and selects the appropriate pan(it may be square,circle etc.).When the cake is ready the Baker responds i.e.gives you back the cake to celebrate your sisters or brothers birthday.I hope this example makes you clear view on how MVC architecture works.

MVC is a collection of Model/View/Controller

Benefits of MVC
  • it keeps your business logic separated from your (HTML-based) views
  • keeps your code clean and neat in one place
  • Represents the information and the data from the database
  • Validation of the data
  • Data presentation and user data
  • Rendering  models into one or more formats, such as HTML,XHTML, XML, or even Javascript
  • Dispatches requests and control flows
  • connects the model with the view
  • Process the data that comes from the model.
I think you would have got a clear idea about MVC.ok let look how RoR uses MVC for producing good web applications.

Ruby on rails uses MVC architecture that we know already, ok lets get cleared that how the MVC is structured for Ruby on rails 3
The above diagram is the MVC architecture of ROR. 

In RoR
this module contains an Object/Relationship Mapping (ORM) library that can be used to abstract how data is actually accessed in the lower level. ORM library maps data stored in a database to a class in an application.
  • handles relationships 
  • handles validations 
  • contains business logic 
  • encapsulates data access 
  • Renders both rhtml and rxml formats 
  • views are implemented using ERb(embedded ruby) by default 
There are three templating schemes known in Rails:

  • .rhtml, generates HTML views to users.
  • .rxml, lets developers construct XML documents using Ruby code, used widely to generate feeds in Rails-powered blogging engines.
  • .rjs, allows developers to create dynamic JavaScript codes, suitable for creating an AJAX interface.


  • Controls the application flow 
  • Controls the model and view 
  • managing user sessions
  • routing external requests into internal actions
  • managing Rails caching features
  • managing helper modules

Apart from the above structure rails do contain one more structure called HELPER

Whats the need of Helper

When you have code that you use frequently in your views or that is too big/messy to put inside of a view, you can define a method for it inside of a helper. All methods defined in the helpers are automatically usable in the views.

Best practices
  • fat model and skinny controller
  • business logic should always be in the model
  • the view should have minimal code
  • Use helpers

No comments:

Post a Comment

fuzionpro Headline