Ruby on Rails (RoR) is a framework written in the Ruby programming language that implements the Model — View-Controller architectural template for web applications, and also provides their integration with a web server and a database server. It is open source software and is distributed under the MIT license.
Created by David Heinemeyer Hansson based on his work at 37signals on the Basecamp project management tool and released in July 2004. On December 23, 2008, the Merb project team teamed up with the Rails team to create the next version of Rails 3, which will combine the best features of both frameworks.
It is based on the following principles of application development:
maximum use of reuse mechanisms that minimize code duplication in applications (the principle of Don’t repeat yourself);
by default, configuration conventions are used that are typical for most applications (the Convention over configuration principle) — an explicit configuration specification is required only in non-standard cases.
Schematic representation of the model-view-controller architecture with additional components
The main components of Ruby on Rails applications are the model, the view, and the controller. Ruby on Rails uses a REST-style of building web applications.
The model provides the rest of the application components with an object-oriented display of data (such as a product catalog or a list of orders). Model objects can load and save data in a relational database, and also implement business logic.
Rails 3 uses the ActiveRecord library to store model objects in a relational DBMS by default. A competing analog is DataMapper. There are plugins for working with non-relational databases, for example, Mongoid for working with MongoDB.
The view creates a user interface using the data received from the controller. The view also passes user requests for data manipulation to the controller (as a rule, the view does not directly change the model).
In Ruby on Rails, the view is described using templates of ERB HTML files with additional inclusions of Ruby code fragments (Embedded Ruby, or ERb). The output generated by the embedded Ruby code is included in the template text, after which the resulting HTML page is returned to the user. In addition to ERB, it is possible to use about 20 more template engines, including Haml.
A controller in Rails is a set of logic that is triggered after the server receives an HTTP request. The controller is responsible for calling the methods of the model and starts the formation of the view.
The correspondence of the Internet address with the controller action (route) is set in the config/routes.rb file.
The controller in Ruby on Rails is a class inherited from ActionController::Base for classic applications and ActionController:: API for API. The open methods of the controller are the so-called actions. Actions often correspond to a separate view. For example, at the request of the admin/index user, the index method of the AdminController class will be called and then the index.html.erb view from the views/admin directory will be used.