Ruby on Rails – Understanding the Model View Controller (MVC) Architecture


Rails provides tools to build an MVC architecture:

Models are classes providing data management. In general the structure of these classes is determined automatically by rails from a database.

The relationships between tables is explicitly specified (has_many belongs_to). Specifying these relationships allows ActiveRecord preload elements children or parent classes.

Views correspond to the way of displaying information to the user. It is usually a combination of HTML and Ruby in Html.erb. It is also possible to program in pure Ruby with Builder.

Finally, there are a multitude of plugins systems that write basic HTML, the best known is the HAML.
Controllers react to user actions, they will look for data in the database and make available views.

Rails can quickly create skeletons of a controller, a model or a view. This is usually scaffold type code that provides common operations on the model such as adding, editing and deleting records.

These initial codes can be adapted to the specific needs of the application.

Rails and Ajax

AJAX allows for dynamic pages with JavaScript and XML / JSON to send requests to the server without reloading the page in the browser. Rails provides several ways to simplify the use of AJAX. The contribution of RJS is to allow the generation of javascript code from a Ruby code.

Web server

Rails can work with WEBrick, the HTTP server included with Ruby. This method is often used for development and testing. In production, Apache or lighttpd or nginx with Mongrel or thin are recommended. It is also possible to use Unicorn, which is very efficient in a dedicated environment.

Email Management (sending and receiving)

Emails are generated in the views. You can create a view in text format, and HTML view, sent in the same email.

Managing attachments is also supported and easy to use.

As regards the management of incoming mail, it is possible to create a virtual user postfix or sendmail. But the easiest is to control a mailbox via IMAP with native ruby library . Managing incoming email attachments using the same principle.


Several management systems database are supported: SQLite , MySQL , PostgreSQL, DB2 , Oracle, Microsoft SQL Server and MongoDB .

By default, SQLite3 is used .


Ruby on Rails is distributed by RubyGems : gem install rails. The source code is also available git.

Instant Rails distribution offers a pre-configured for Microsoft Windows Ruby, Rails, Apache and MySQL .

Locomotive project proposes a simplified system for Mac OS X. But RoR is included in the client and server versions of Mac OS X 10.5.

Ruby on Rails is also included in many GNU / Linux distributions.

Finally, RVM can install multiple versions of ruby and rails on the same machine.


GNU Emacs
Rinari a Rails mode for Emacs.

About the Author