Web Hosting Glossary -
Ruby on Rails
Ruby on Rails
Ruby on Rails, often called RoR, or just Rails, is an open source web application framework written in Ruby that closely follows the Model-View-Controller (MVC) architecture. It strives for simplicity and allowing real-world applications to be developed in less code than other frameworks and with a minimum of configuration. The Ruby programming language allows for extensive metaprogramming, which Rails makes much use of. This results in a syntax that many of its users find to be very readable. Rails is primarily distributed through RubyGems, which is the official packaging format and distribution channel for Ruby libraries and applications.
Ruby On Rails' guiding principles include "Don't Repeat Yourself" (DRY) and "Convention Over Configuration."
"Don't Repeat Yourself" means that definitions should only have to be made once. Since Ruby On Rails is a "full-stack" framework, the components are integrated so that bridges between them need not be set up manually. For example, in Active Record, class definitions need not specify the column names; Ruby can already find them from the database itself, so defining them in both the program and the RDBMS would be redundant.
"Convention Over Configuration" means that the programmer only needs to define configuration which is unconventional.
For example, if there is a Post class in model, the corresponding table in the database is posts, but if the table is unconventional (e.g. blogposts), it must be specified manually (set_table_name "blogposts").
Rails' MVC architecture
The pieces of the Model-View-Controller (MVC) architecture in Ruby on Rails are as follows:
In object-oriented, database-driven MVC web applications, Model consists of the classes representing RDBMS tables.
In Ruby on Rails, Model classes are handled through the Active Record. Usually, all the programmer needs to do is to subclass the ActiveRecord::Base class, and the program will automatically figure out which RDBMS table to use and what columns the table has.
The class definitions also specify the relations between classes with object-relational mapping commands. For example, if the class name Image has a definition has_many :comments, and there is an instance of Image named a, then a.comments will return an array with all Comment objects with image_id equal to a.id.
The data validation handlers (e.g. validates_uniqueness_of :checksum) and any update-related handlers (e.g. after_destroy :remove_file, before_update :update_related_details) are also specified and implemented in the model class.
In MVC, View is the display logic, or how the data from the Controller classes is displayed. In web applications, this frequently consists of a minimal amount of code, interspersed in HTML.
There are currently many ways the views can be handled – the underlying view code is part of the Action Pack. The method in Rails itself is to use Embedded Ruby (.rhtml files), which are basically fragments of HTML with some Ruby code interspersed, with syntax quite similar to JSP. HTML and XML can also be constructed programmatically with Builder or through Liquid template system.
For each method in the controller that needs to display user output, a small HTML code fragment needs to be written. The page layout is described separately from the controller action that displays layouts, and the fragments can also call other fragments.
In MVC, Controller classes respond to user interaction and call the application logic, which in turn manipulates the data in Model and displays the data through View. In web-based MVC applications, the Controller methods are initiated by the user through the web browser.
Controller implementation is handled through Rails' Action Pack, which contains the class ApplicationController. Rails applications simply subclass ApplicationController and write required actions as methods, which can then be accessed through the web, typically in form of /example/method, which calls ExampleController#method, and presents the data using the view file /app/views/example/method.rhtml, unless the method redirects elsewhere.
Rails also provides out-of-the-box scaffolding, which can quickly construct most of the logic and views needed to do common operations, such as CRUD.