Posted September 7, 2010 by Spyros in Ruby on Rails

Ruby On Rails Programming : Introduction and the Model View Controller Model Explained


Lately, i’ve been quite interested in learn more things abour Ruby Programming. To be sincere, i always wanted to learn a bit more about the language, but I’ve been into some WordPress Theme programming the past 2-3 months. Hopefully, i will write some posts about this subject as well, in the future.

What is Ruby on Rails ?

Ruby, as you may know, is a very well known scripting language, with an exceptional and clean syntax. I’ve always been a fan of Python and i must say that moving to Ruby is a totally different programming style. Luckily, learning Ruby is quite easy. It indeed offers some very good techniques, like code blocks etc, but overall, it resembles interpreted language, like Python and Perl also are.

On the other side, Ruby on Rails is something completely different and harder to learn. RoR (widely used for Ruby on Rails), is actually a framework for creating web applications. As simple as that. You may already know the Django alternative for Python. Sounds … ok, but is it just that ?

Why Ruby on Rails is Really Really Good

RoR is really great because it makes the life of the programmer a WHOLE lot easier. Imagine that you wanted to write a php function that checks whether  passwords match in a simple registration routine. Using php, you would need to get the input, compare it and write an html page that presents the error to the user. Lots of work for doing a simple thing. But with ruby :

validates_confirmation_of :password

is just enough !

Installation of RoR

Installing RoR is a pretty easy procedure for the well known operating systems. There is an installer for Windows, you can use a package manager for Linux and macports for mac OS (as i currently use). For more information on the whole process, please take a look at the official ruby programming website.

The MVC (Model – View – Controller) Model.

MVC is the programming model that RoR utilizes. If you have used a templating system like smarty in the past, you have most probably already used the model. This model simply identifies that our web program has 3 different entities that we need to look after. The model, the view and the controller. But, what is the difference ? Now, i could describe that in words, but i prefer providing an example as well :

The View is the easy entity. It is what our program prints out to the browser of a user.

The Controller closely relates to data. It is the part where data is initialized, to be then provided to the View and shown to the user.

The Model is the auxiliary entity where we write our large functions that operate on data.

Now, think of this situation. You have a page that presents some products for people to buy. The Controller is the first thing that gets utilized. Once the browser requests the page, the controller takes action. Therefore, when the products page is requested, the Controller understands that this page was called and provides the gateway between your large functions and your view. Now, the Model contains functions such as getAllProducts(). This function is called by the controller, to provide an instance of all the products. This variable, most times a class variable like @products, is going to be used in the view.

Hope this starts to make sense, i’ve tried to describe it to the best of my knowledge. I do not want to take this post any further. In my next one about RoR, i will describe how to create a test project, use migrations and more. Have fun with Ruby, play a little bit with it, it is really worth it, you have my word !