📜 ⬆️ ⬇️

Ruby on Rails step by step. # 1 Theory

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:
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:


Here is the MVC diagram:
image
Based on this, RoR uses three components:

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:


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:

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:


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.


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)

Source: https://habr.com/ru/post/49700/


All Articles