Having briefly set aside a series of articles about YaB Ruby to the side (
1 ,
2 ,
3 ,
4 ,
5 ,
6 ,
7 ,
8 ,
9 ,
10 ), I decided to present you a new cycle about Rails framework. Having gained some experience in the “training” I will try to bring this series to a level of quality and reasonableness somewhat higher than before.
The goal of the first part of the lessons on Ruby on Rails will be to create a multi-user blog (ala Habr). I would also like to note that for this first part it is desirable to have knowledge of Ruby at least at the level of three to four drops. I would like to quickly start coding, but I still have to start with the theory.
What is Ruby on Rails (hereinafter referred to as RoR)? The most common answer is the Ruby (further referred to as Ruby) framework, which implements the MVC pattern (hereinafter referred to as the pattern). We distinguish two main points from the answer:
- This is a Ruby-based framework.
- It implements the MVC pattern.
Let's sort each separately.
RoR - Ruby-based framework
YP Ruby - simple and powerful, the possibility of meta-programming, blocks, iterators, as well as exception handling makes the language a wonderful basis for the framework. Actually, David Haynemeyer Hansson, the creator of RoR, considered it and in July 2004 the framework came to light.
You can see how Ruby’s meta-programming helps in the ORM component of RoR Active Record. Based on the
class name, RoR reads the schema (schema) and on the fly creates class
objects based on the database table. You can go to the next aspect.
')
RoR implements MVC
Perhaps you have already heard about MVC in relation to other frameworks, but what is MVC in RoR? MVC is an application architecture pattern that clearly separates its three components:
- M odel (hereinafter Model ) is the "essence" of the application and is responsible for the direct algorithms, calculations, and the like, the internal structure of the application. Also provides a link to the data warehouse.
- V iew (View, farther View ) is designed to display the data provided by the Model. This is the only part of MVC that directly contacts the user.
- C ontroller (Behavior, hereinafter Controller ) receives data from the user and transfers them to the Model. In addition, he receives messages from the Model and sends them to View.
Here is the MVC diagram:

Based on this, RoR uses three components:
- Active record
- Action view
- Action controller
The combination of the last two is known as the Action Pack. Let's take a closer look at each component and find out why Ruby is so suitable for implementing the framework.
Active record
Active Record is a Model in RoR. The model stores data and provides a base for working with data. In addition, Active Record is also an ORM framework. ORM means Object-relational mapping (Object-relational projection). Actually Active Record does the following things:
- The projection of the table on the class . Each table is projected onto one or more classes according to the convention over configuration principle (agreement above configuration). One of these conventions is that the name of the table should be in the plural, and the name of the class in the singular. The attributes of the table on the fly are projected into the attributes of the Ruby instance . After all the projections are made, each ORM object of the class represents a specific row of the table from which the class was projected.
- DB connection . You can connect to the database using the API provided by Active Record, which creates the query you need directly into the database engine using adapters. Active Record has adapters for MySQL, Postgres, MS SQLServer, DB2, and SQLite. It is only necessary to record the database access parameters in the database.yml file.
- CRUD operations . These are operations c reate (creation), r etrieve (receiving), u pdate (updating) and d elete (deleting) above the table. Since Active Record is an ORM framework, you always work with objects . To create a new table row, you create a new class object and populate its instance variables with values. It is worth noting that Active Record does all this for you.
- Data validation . Checking the data before placing it in a table is the first step in the security of your project. Active Record provides model validation. The data can be checked automatically using a variety of ready - made methods , which, if necessary, can be rewritten to fit your own needs.
Action view
The view includes the logic necessary to output the Model data. The Role of the RoR is played by the Action View. The most commonly used Action View features are:
- Templates . Templates are files containing placeholders (placeholders) that will be replaced with content. Templates can contain HTML and Ruby code embedded in HTML using embedded Ruby ( ERb ) syntax .
- Helpers (helper, further helper) of forms and formatting . Form helpers allow you to create such page elements as checkboxes, lists, using ready-made methods . In turn, formatting helpers allow us to format the data we need, methods exist for dates, currencies and strings.
- Layout . Layouts determine how the content will be located on the page. A dynamically created page can contain an attachment of several pages, even without using tables and frames, using the Layout API .
Action controller
In a web application, the Controller controls the flow of application logic. It is located on the border of the program, intercepting all requests, on the basis of which it changes some Model object and calls View to display the updated data. In the RoR Action Controller is a controller, here are its main functions:
- Session support . A session is a period of time spent by the user on the site. It can be tracked using a cookie or session object . A cookie is a small file; it cannot contain objects, unlike a session object.
- Filtering There are situations when it is necessary to call a certain code before executing the Controller logic or after it, for example, user authentication, event logging, providing a personal response. The filters provided by the Action Controller help in such cases. There are three basic filters: before, after and around. About them - later.
- Caching Caching is the process by which the most requested content is cached so that there is no need to request it again and again.
Three Wednesdays
RoR encourages the use of separate environments for each of the stages of the application's life cycle: development (development), testing (testing) and exploitation (production), for each of which a separate database is created. Consider every Wednesday.
- development . In the development environment, the bet is placed on the immediate display of the new version when the code changes - it is enough to refresh the page in the browser. The speed in this environment is not important. When an error occurs, it is displayed.
- test . When testing, we usually fill the database with some stupid text each time to make sure that the normal behavior does not depend on the content of the database. Procedures for unit testing and functionality testing in RoR are automated and are performed via the console. The test environment provides a separate space in which these procedures operate.
- production . In the end, your application goes to the final line by passing tests and getting rid of bugs. Now code updates will rarely occur and you can concentrate on performance, turn on caching. There is no need to write huge error logs and frighten users with reports of these errors in the browser. For you - the production environment.
Epilogue
Yes, yes, and here the epilogue is nowhere without it;) Today, you and I learned about MVC, how it is implemented by Rails, learned the role of the framework components, learned how they communicate with each other. They even looked at the different environments that Rails provides us with. If everything is clear, then you can begin to equip the workplace! Comments are eager;)
PS: This number is based on the book
Building Dynamic Web 2.0 Websites with Ruby on Rails . However, with the literature on Rails it is worth being as careful as possible, since the framework evolves at an elusive pace, and almost all books (even 2008, including this one) are based on older versions of Rails (1.2.x)