First slice of rails

    “What are you talking about, Morpheus?”

    The number of frameworks in the world is growing at an alarming rate. Thousands of them are already at the moment. Fortunately, there are not so many best of the bottom. The whole world of Web developers has simply divided into several groups that will give preference to a particular framework. I do not like chicks and holivars, so God bless him with what is better. I just want to talk to ASP.NET developers about Ruby on Rails.

    What for?

    I'll tell you my story. I have been writing ASP.NET since 2004. I know the many nooks and crannies of this system and I know that it is amazing in itself. I survived 3 large projects on .NET and wrote from 10 sites that now continue to work stably. If we talk about frameworks, then .NET satisfied me all. In the largest projects, I found solutions to the most complex problems. And now, after 6 years, I just wanted to see, but what else is there in the market? Naturally, the first thing I came across RoR.

    The article is designed for experienced programmers. If you can not distinguish the compiler from the interpreter, then you better google.

    What interested me? I watched this screencast . It smartly talks about how you can create a blog in 15 minutes using RoR. Okay, I decided, if you say that everything is so joyful, it's worth a try.
    Lyrical digression. In order for Windows owners to watch a mov file without installing QuickTime, they will have to get an alternative version of Quick Time and install Media Player Classic . Most ruby ​​screencasts are made on the Mac OS. I myself am a burnt breeder, and I had to tinker a bit. Again, this is not an article about holivars.

    Back to the topic.

    Talk about terminology and what we need

    Ruby is a high-level interpreted language for fast and convenient object-oriented programming. The language has an operating system-independent multithreading implementation, strict dynamic typing, a garbage collector, and many other features. Ruby is similar in syntax to Perl and Eiffel, in object-oriented approach to Smalltalk. Also, some features of the language are taken from Python, Lisp, Dylan.

    Any programmer who has been developing on .NET for a year is able to understand and quickly learn Ruby. And there are quite a few articles written on this subject. We will not stop .

    You can install Ruby on Windows using One-Click Install.

    I note that unlike the usual% APPDATA% directory, everything about Ruby is better to put on the C: drive or something like that. It will simply facilitate further work with the system.

    I’ll say right away that Ruby without plugins is like a ruby ​​uncut. Therefore, Gems was developed.
    RubyGems is a package manager for the Ruby programming language that provides a standard format for Ruby programs and libraries (in the self-contained “gems” format), tools designed to easily manage the “gems” installation, and a server for distributing them.

    In fact, I cannot find in .NET any such replacement as a normal package manager. The concept of .NET is such that everyone makes an installer and distributes packages for .NET in a form pleasing to him. In Ruby, everything is much simpler and better. For example, there is a site, which is gaining popularity in Russia. For, an API is written that can be used in your programs. Someone already wrote a wrapper for this API. Excellent! Here we can use it by typing the following command in PowerShell:
    gem install facebooker

    Regarding what it means to type gem install facebooker command. Just me, as for an avid breeder, it always led to a stupor. Because you had to look for the gems executable. All you need is in the C: \ Ruby \ bin folder. I

    immediately recommend updating this Gems to the latest version, because in the future it will be very useful.
        PS C: \ Ruby \ bin> gem update --system

    Just a little more terms, and you can start programming.

    Ruby on Rails is a software framework written in the Ruby programming language. Ruby on Rails provides an architectural model of Model-View-Controller for web applications, as well as their integration with a web server and a database server.
    Provides a homogeneous environment for the development of dynamic AJAX interfaces, with request processing and data output in controllers, domain reflection in the database.
    Ruby on Rails is open source software and is distributed under the MIT license.

    And here is our hero! In fact, if you still do not understand how Ruby on Rails differs from Ruby, let me offer you the following equation, which will help you figure it out.
    dotNET => 'ASP.NET Framework'
    Ruby => 'Ruby on Rails'

    I think it will be more understandable. And so, we have a language, we have a framework that allows you to work in this language, creating WEB applications. A little more and you can start.
    For, after we installed and configured the framework, we only need to start the server, and check how it works. Here I can start talking about how to install Apache, sqlLite or MySQL on the system ... But this is too long and dreary. There are two options for making life easier:
    1) Instant Rails- This is a server already configured for operation, which includes Apache, the Sequel and Ruby with Rails. Unpack the archive to disk, start the batch file, it swear a couple of times and configures Apache and everything that your heart desires. The plus is that the installation of all this happiness is extremely simple. Even DenWER, in comparison with this joy, seems terribly confused. I note that this option is ideal for those developers who consider Notepad ++ the best development environment.

    2) Aptana - this is needed by those who cannot live without Visual Studio or Sharp Develop. This is a full-fledged studio that allows you to write on RoR and greatly simplifies the life of a programmer.

    I warn you: the studio will need to be put in a folder that does not contain Russian characters in the directory names. For some parts of the framework do not understand what Russian means. I recommend putting it as close to the disk as possible.

    The studio itself will offer to put Ruby, if you haven’t done it before. Also, Aptana will notice that you need Firefox 1.5 and higher to debug the javascript.

    Everything is your choice. I will work with Aptana, as she made life easier. I must say right away that before you begin, you will need to update Gems, as described above.

    For the happy owners of Windows 7 - Aptana works stably only under Admin rights. Which I recommend to you.

    Well, it seems everything is told, you can start the project.

    In Aptan, this is all done with the right mouse click in the Rails Explorer window. Once again, I note that the entire project is best placed as close as possible to the root of the disk and in folders without Russian characters.

    If you are a seasoned programmer, then you can tinker with the rails and lay them to yourself through the console, and not through the right mouse button.
    PS C: \ Ruby \ bin> rails testblog

    Actually, in essence, the teams are exactly the same. Both of them will create the framework of the site that you will write. The difference is that Aptana will also launch a server for this business. By the way, if she doesn’t do this, then you can still fix it by clicking on Window> Show view> Servers

    Well, now at http: // localhost: 3000 / we have an absolutely unnecessary page, which gladly says that everything works well for us. Although, in fact, on this page there is a tutorial on how to properly make your first database and access to it. Let's follow it, I’ll just add some data so that you don’t get confused at all.

    And so, here we are creating a new project:

    And now, we have created it:

    Just want to draw your attention to a couple of facts. Firstly, if you really always wrote only under .NET and never saw Apache, then know that the root of any site is the public folder. Marked in blue. Rails generously endowed this folder with various error page stubs, index pages, and so on. Well, this is very convenient.

    Just for future generations, I note that in Ruby much attention is paid to the automatic testing system. The test folder is marked in purple. Just know that testing your applications is really important, and remember that Ruby allows you to automate this routine well.

    The main joy of the Rail is marked in red. This is the Arr folder, which contains the 3 most important folders - controllers, models and views.
    Strictly speaking, MVC is the essence of all Rail. After all, the application is built on them. Let's build a small piece of this application.

    If you don’t find such a tab, click Window> Show View> Genarators.

    Now we will follow the example from the page http: // localhost: 3000 / and generate a scaffold recipe title: string author: string description: text

    Soooo. I think this is where the meaning begins to get lost. What is a scaffold? Here, just with this and it is necessary to understand! In my opinion, a scaffold is one of the greatest goodies of Rails. In my life I have seen a lot of means to access data. I wrote two self-made frameworks that allowed you to manage data. I used the ASP.NET Entity Framework, but ... Scaffold impressed me with its simplicity and power. Let's go in order.

    scaffold - scaffold; chopping block; gallows; execution
    to go to / mount the scaffold - lay his head on the block; end life on the gallows
    to bring to the scaffold - bring to the gallows
    to send to the scaffold - sentence to death

    And here it’s not without jokes. I don’t know what the developers were thinking, but the translation of this word worries me.

    scaffold - scaffolding
    The house was scaffolded to the very attics. - Forests were lined up to the attic of the house.
    2) support, support, carry (on itself) the load
    3) place (products) on high stages (for drying, protection from animals)

    That's better. A scaffold is something that carries a load. In this case, a scaffold is what takes on the functions of access to the database and completely deprives the programmer of all brain torment.

    What kind of team did we score?
    Entity: recipe
    Field: type
    title: string
    author: string
    description: text

    And so, we created a new entity in the data access layer.
    In the db folder, the migrate folder was created, in which a script appeared that allowed us to migrate our entity to the final database.

    class CreateRecipes <ActiveRecord :: Migration
      def self.up
        create_table: recipes do | t |
          t.string: title
          t.string: author
          t.text: description
      def self.down
        drop_table: recipes

    This class was automatically created by the framework, and has two methods. Up and Down. These methods will be used to roll back to the base, and roll back from the base. That is, if you make changes to your scaffold several times, then several versions of the migration script will be created that will invoke the Up method in sequence. This allows you to keep your database under control and normally roll it onto server products.

    Moreover, please note that this is not SQL. This means that at the moment we are absolutely DB-independent.
    Please open config / database.yaml and look at its contents.
    # SQLite version 3.x
    # gem install sqlite3-ruby (not necessary on OS X Leopard)
      adapter: sqlite3
      database: db / development.sqlite3
      pool: 5
      timeout: 5000
    # Warning: The database defined as "test" will be erased and
    # re-generated from your development database when you run "rake".
    # Do not set this db to the same as development or production.
      adapter: sqlite3
      database: db / test.sqlite3
      pool: 5
      timeout: 5000
      adapter: sqlite3
      database: db / production.sqlite3
      pool: 5
      timeout: 5000

    Really nice? You can even make different types of databases on different servers.

    Okay. We have an entity, we have a description of the database, but so far we do not have the database structure for this entity. Therefore, we right-click on the project, select Rake> db> migrate

    A little more theory. What is a rake?
    Rake is a tool for automating the assembly of program code. It is similar to SCons, Make, and Apache Ant, but has several differences. This tool is written in the Ruby programming language, and so on. Rakefiles (Makefiles analogues in make utility) use Ruby syntax. The author of Rake is Jim Weirich.
    Rake uses blocks of anonymous Ruby functions to define various tasks using Ruby syntax. It has a library of basic tasks, such as functions for file manipulation tasks and a library for deleting compiled files (the “clean up” task). Like Make, Rake can also synthesize tasks based on templates (for example, automatically assembling a task to compile a file based on file name templates).

    And so, what we did - we made rake migrate our spherical entities in a vacuum to the database. It is very nice of him. It remains only to write an access code to this entity and that’s it, you can enjoy life.

    But here lies the main joke of Rails. Scaffold is not a fool. And not only provided us with an entity access code, but also wrote an MVC for it. We can safely go to localhost : 3001 / recipes and see what we have there. And we have a standard type of access to the database, which allows you to make basic changes and work with the database. You can play around.

    What happened? Where is the code and how does it work? Everything is in place!
    Go to the App folder and see what lies in the models, controllers and views of your project. You already have everything you need to work with the entity. I want to note that in Rails a lot is tied to the names of variables and files. So, by studying recipes_controller.rb you will find a lot of simple code that is not originally associated with views, but the comments indicate which files the framework will access.

    Well, now it’s time to give the programmer all the code in his playful pens and give it a treat.

    After all the data that I have outlined, you can review the video file that I gave as an example at the beginning of the article. It will be much more understandable.

    For further study, I recommend: A
    delightful six-hour lecture at Berkeley. (Part one)

    A collection of more than two hundred screencasts on Ruby:

    For a much deeper understanding of what I have stated above, I recommend this article

    Thank you to Wikipedia for the quotes.
    Thank you for your attention. I hope this article gives you the opportunity to taste the delicious RoR.

    This article is made especially for Habr and only for Habr.

    Also popular now: