Ruby on Rails The MVC framework for complex web applications

Tino Jose
Oct 19 27 min read

In 2004 the American web development company 37signals (today Basecamp) released the project management application Basecamp, which included features such as to-do lists, time tracking, or a messaging system. Part of the software was a basic architecture, which was created especially for this purpose by the participating programmer David Heinemeier Hansson but was extracted in the same year and published in 2005 as an independent and open source web application framework. The name of the basic framework developed in Ruby and now well known: Ruby on Rails (“RoR” or simply Rails). At that time, nobody could have guessed that the framework, which is now maintained by a team of several developers, would one day become one of the most sought-after solutions for the creation of new web applications.

What is Ruby on Rails?

Ever since Ruby on Rails was released under the MIT license, developers have continued to develop the framework. They have remained true to their philosophy: “Don’t repeat yourself” (DRY). Each piece of information should only exist once in a project based on the Rails framework. For example, it is sufficient to define the columns of a table only in the database without storing this information in the source code or a separate configuration file. The implemented Active Record module reads the information directly from the database.

The second design paradigm is “convention over configuration.”. The RoR framework thus provides certain conventions, for example, for naming classes. If developers adhere to these conventions, they save a lot of configuration effort. However, Ruby on Rails also allows alternative configurations, so you have complete flexibility when programming your web app.

Ruby: The flexible and platform-independent language of the Rails framework

In the mid-1990s, the Japanese Yukihiro Matsumoto published the object-oriented language Ruby. Initially used almost exclusively in Japan, the script language has since established itself worldwide as a popular alternative to the industry leaders PHP, Python, and Co. This success is no coincidence: Matsumoto’s main goal during development was to ensure that programming is fun in the long term and therefore combines the best features of other languages in Ruby. As an interpreted scripting language, the code is executed by an interpreter, which is a small speed disadvantage compared to compiled scripts, but on the other hand, makes Ruby much more flexible and dynamic. Since specific interpreters exist for all common operating systems, Ruby code is platform-independent, thanks to this fact.

A significant feature of Ruby is its multi-paradigm capability, which is also a feature of C++, for example. So you are not bound to a specific programming paradigm. Thanks to the “principle of least surprise”, you can use the scripting language intuitively and usually without encountering unexpected behavior. As open-source software under the free BSD license, Ruby enjoys a large and very active community that has contributed numerous current program libraries, most of which are also covered by this license and play a major role in the development of modern web applications. They can be installed, used, and updated with the help of the so-called RubyGems. One of these gems, which also includes the public web APIs of Google and Facebook, is the Rails framework.

Model-View-Controller architecture (MVC)

Ruby-on-Rails runtime environments are closed systems. They contain the interpreter, the necessary program libraries, and the respective scripts. Each Rails project is subject to an automatically generated directory structure that separates scripts, configurations, classes, content, etc. from each other. Thanks to this structural approach of managing data and its adaptations and presentation form separately, the web applications are easy to maintain. One also speaks of a model-view-controller architecture, whose layers can be described as follows:

Model

Usually Rails applications are linked to relational databases. To communicate with the respective database management system and to create or manipulate entries, the Ruby framework requires models. These models map classes to a database table and individual attributes to the appropriate columns. By default, the model layer is based on the ORM (object-relational mapping) Active Record framework. Since Rails 3.0 you can also use other ORM libraries like Sequel thanks to the implemented plug-in API.

Views

The view-layer or presentation-layer needs the framework to access the data from the model-layer and to display it visually. For this purpose, Ruby on Rails uses the Action View class, which supports various output formats. Rendered as HTML documents, views present the corresponding data to the requesting user, for example. XML or JSON documents are used to make the database records accessible to other programs or services.

Controller

Controllers are the interface between the model and the view layer. They process the incoming requests through the web browser, retrieve the corresponding models from the database, and forward them to the views that provide the visualization. As central control units of the Rails application, controllers also manage the caching behavior or combine different requests of a single client, which are made over a period of time, into one session.

The standard components of the RoR framework

As a framework for web applications, Ruby on Rails provides all the components you need as a developer to program a feature-rich application in its standard configuration. The only additional requirements are that Ruby and the package manager RubyGems as well as a SQL database are installed. For the latter, the Rails team recommends the open-source SQLite library written in C, which includes a relational database system. These are the standard modules of the Ruby on Rails framework:

  • Action Controller: The Action Controller module is used to create the mentioned interfaces between the database entries and the accessing users and programs.
  • Action View: Action View offers you the possibility to create views for the individual entries and controllers. About naming, the convention exists that all views must be named after the respective controller.
  • Active Record: Active Record is the central module for manipulating the database of your Rails application. To create the individual models with it, you need to configure very little if you follow the conventions regarding naming and schema. Ruby on Rails, with its powerful scaffolding system, is the ideal tool for creating interfaces for the four CRUD operations that present the database on an object-oriented level.
  • Active Resource: This module implements the object-relational mapping (ORM) for REST web services. Like Active Record, this component focuses on saving configurations.
  • Action Mailer: Action Mailer allows you to send and receive e-mails in your web application. In this way, you can easily set up e-mail registration and login.
  • Active Support: The Active Support module contains several useful utility classes and standard library extensions.
  • Railties: To ensure that all components of the RoR framework work together, there are so-called railties. For this reason, each module has implemented its own railties, for example, to start the initialization process and to be able to make configurations to the framework. Railties are also necessary to add Ruby on Rails own modules.

Ruby on Rails: Advantages and disadvantages of the web framework

On wikibooks.org you can find the following joke about the question for the best scripting language:

Two developers talk about programming languages. The first one: “I work with Java, PHP I give my children to play with.” Then the second one: “I play with my kids and make Ruby work for me!”

Of course, there is a good deal of exaggeration in this joke, but in fact, there is also a true meaning. Java is considered a first-class choice for complex solutions and is therefore used in many frameworks in the business area. However, programming with the platform-independent language is anything but easy due to the often very complex structure and is usually very costly and time-consuming. Ruby, as an interpreted scripting language, is also not bound to a specific platform. At the same time, code written in Ruby is much faster, easier to customize, and much smaller in size. However, the interpreter language also ensures that the speed of developed applications is slower than comparable Java applications.

PHP also has significant advantages over Java in terms of simplicity and development effort. In web development, it is no coincidence that the language is regarded as an established standard solution, forming the basis of various Content Management Systems such as WordPress, TYPO3, or Joomla. However, since PHP was rather arbitrarily compiled and not always object-oriented, upgrades of web applications developed with older versions can sometimes be quite tedious. Ruby on Rails was completely object-oriented from the beginning, so applications are written using older versions of PHP come with cleaner code than comparable PHP projects by default.

Ruby on Rails contains an integrated framework that allows you to test the functionality of your application at any time. Thanks to this possibility, errors can be detected and corrected right from the start. Web 2.0 techniques such as AJAX can be used from the factory with just a few lines. Also, Ruby and the Rails framework enjoy a huge and ever-growing community, as evidenced by the vast array of extensions.

The innovative philosophy of Rails aimed at making things as easy as possible for the developer, which Matsumoto has followed from the beginning with features like the scaffolding system, fixed conventions, or the MVC structure, has also served as a model for many newer frameworks. This approach is reflected in the PHP frameworks Symfony, CakePHP, and Zend Framework, among others.

For whom is the Ruby framework suitable?

Ruby on Rails offers everything a developer needs to create modern web applications: First-class structures for implementing the interface, easy to set up the connection to an SQL database, and the ability to use web technologies such as AJAX. The clear principles of “Don’t repeat yourself” and “Convention over configuration” provide the basis for clean, easy to understand, and customizable code that is written quickly. Custom configurations are still possible without any problems because Ruby is designed to give a programmer all the freedom he needs. This makes the framework suitable for both newcomers and experienced programmers who have worked with other scripting languages to date, even if in both cases a certain amount of time should be allowed for getting started.

Thanks to the scaffolding system, prototypes of planned web projects can be realized in a very short time, which is clearly one of the great strengths of the Ruby framework. Thanks to the integrated test environment, you can check your application for possible errors already during the development phase. Ruby on Rails is also well prepared for complex applications. With the help of the extensions called RubyGems, which include the framework itself, you can easily add required interfaces, libraries, or functional and visual features. Since Ruby and the RoR framework are subject to the free BSD license, the only costs you have to pay are for the hosting of your web project.

However, you should not assume that Ruby on Rails does not require you to have any programming skills. Of course, the scripting language doesn’t do all the work for you, as the Ruby developer claims in the joke above. In terms of speed, the developed RoR applications cannot quite keep up with the competition. The code is only executed when users access the respective content, so logically the processing of incoming requests takes a bit longer than with pre-compiled lines of code.

Furthermore, if you want to organize your application with content management systems like Drupal, Joomla, or WordPress, you have to do without the Rails framework. Like many other platforms, the above examples are based on PHP. But there are also already CMS solutions based on Ruby on Rails like Alchemy CMS. Forecasts about the spread of RoR and the chance to jeopardize the status of the market leaders PHP, Java, and Python are difficult to make but the advantages and increasing popularity of the Ruby framework speak for themselves.

Written by

Tino Jose
Oct 19 27 min read

Related Article