📜 ⬆️ ⬇️

First introduction to Ruby on Rails

Ruby on Rails (hereinafter simply rails) is a web framework written in the Ruby language.
On Habré there are several articles about the language, I think it will be useful to read them if there are difficulties - read Wikipedia , the best book about the language - Programming Ruby , the best book about rails - Agile Development with Rails , and ask questions.

Let's get started with installing Ruby and Rails.



Windows users need to download and install the One-click Ruby installer .

Linux users (Ubuntu) install Ruby even easier:
')
  <code class = 'sh' lang = 'sh'> $> sudo apt-get install ruby ​​rubygems rake </ code> 


Make sure Ruby works

  <code class = 'sh' lang = 'sh'> $> ruby ​​-v
 ruby 1.8.5 (2006-08-25) [i486-linux] </ code> 


and put the rails:

  <code class = 'sh' lang = 'sh'> $> gem install rails - include-dependencies
 $> rails -v
 Rails 1.2.3 </ code> 


All you need to start is a text editor with Ruby syntax highlighting and the ability to execute commands from the console.
For IDE fans, I can advise Aptana , I write code in it (I just like Eclipse :).
All the examples I will give for the console, what to do in Aptana should be clear, I can write about it separately.

Now everything is ready to start a new project.



The project on rails begins with the creation of the skeleton of the project:

  <code class = 'sh' lang = 'sh'> $> rails ~ / projects / example </ code> 


The example folder and the project skeleton in it will be created (do not forget to replace the path with your own). Let's see what's inside.



It's time to start the web server and make sure the application is working. Go to the folder with the application and run the web server that comes with the rails:

  <code class = 'sh' lang = 'sh'> $> ruby ​​script / server
 ruby at the beginning of the line you need to write only in Windows. </ code> 


http: // localhost: 3000 /

Open this link in your favorite browser and watch the “Welcome aboard” page - it means everything is good and it's time to write a simple application.

What application should write?



The fact is that I can write text with human markup (Wiki, Markdown, Textile), but Habr only supports html for text markup, but it’s not convenient for me to write html when I just want to write text.
Therefore, the application will format the text in html!

Initially it will be very simple, you will need only two pages: on the first you can enter text, and on the second you can look at it in formatted form and copy the resulting html (to post it on Habr :).

Start writing code.



For the lazy, I laid out the source code of the application , but still it is more interesting to write the code myself.

I think it’s no secret to anyone that the MVC pattern has become the de facto standard of web application architecture. Rails are no exception, the application consists of models, views and controllers.

Now and in the future, we will use scripts that will do all the routine work for us, the first with which we will learn to generate , it generates code.

  <code class = 'sh' lang = 'sh'> Create a controller (I called it formatting_controller) with two methods (views):
 input - for text input  
 preview - to view formatted text

 $> ruby ​​script / generate controller formatting input preview </ code> 


As a result, the rails generated several files of which the following are now interesting:

  <code class = 'sh' lang = 'sh'> app / controllers / formatting_controller.rb
 app / views / formatting / input.rhtml
 app / views / formatting / preview.rhtml </ code> 


How to see the generated pages in the browser?

http: // localhost: 3000 / formatting / input

http: // localhost: 3000 / formatting / preview

Why such urly?



Routing module is responsible for the appearance of urls in rails, you can configure it in the file

  <code class = 'sh' lang = 'sh'> config / routes.rb </ code> 


Now I will not go into the details of routing, you can write a separate article about it. Now just add these two lines and I will explain their purpose.

  <code class = 'ruby' lang = 'ruby'> map.connect '',: controller => 'formatting',: action => 'input',: conditions => {: method =>: get}
 map.preview '',: controller => 'formatting',: action => 'preview',: conditions => {: method =>: post} </ code> 


connect and preview are method calls. The first argument is the path, in our case the path is empty, that is, the rules for the root of the site. The second argument is a hash that contains which method of which controller should be called and under what conditions. Ie, if HTTP GET comes for the site root, then the input method will be called, and if HTTP POST, then preview.

How does connect call differ from preview call?
Connect is a method that adds a rule for a router, but the preview method does not exist.
This is a common case for Ruby, in the case where the method does not exist, method_missing is method_missing in which you can do useful things. In our case, the following happens:



Now you need to delete or rename public/index.html so that instead of this page, the input method is called and check that it is actually being called.

It is time for the view to show something more useful.



Let's start with input.rhtml , replace it with this code:

  <code class = 'ruby' lang = 'ruby'> <% form_tag preview_url do%>
 <% = text_area_tag: text, @source,: size => '120x25'%>
 <% = submit_tag 'Preview'%>
 <% end%> </ code> 


It turned out a page with a form that will be posted on the preview_url , inside the form there is a text field and a button to send. Now we undertake formatting the entered text.

First you need to decide how to format the entered text. I format this article as markdown , and for a change I decided to add textile .
To support textile and markdown, you need to install RedCloth and Maruku .

  <code> $> gem install redcloth maruku </ code> 


Now you need to write code that will format the text. Unfortunately, I can not place the code in the article, because Habr cuts it, so it is better to open formatting_controller.rb in another window and switch as you read. What's the point?



What was set_supported_formats needed set_supported_formats and what do all these @ mean?
Variables with an @ sign are at first variables of the object for which the method is called — that is, the controller. The fact is that the view has access to the controller's variables, as it is its own variables, so usually the variables are set in the controller and in the view they are used for rendering. In preview.rhtml we will also render the input.rhtml input.rhtml so that you can edit the text, so @supported_formats will @supported_formats required in both views, it’s better to add a filter than to duplicate the code.

It remains to add the choice of the type of formatting in input.rhtml :

  <code> <% = select_tag: selected_format, options_for_select (@supported_formats, @selected_format)%> </ code> 


and write preview.rhtml , again, I can not place the code here (there is a div, and it does not matter that it is inside pre :).

The page is divided into three blocks:



One small detail remains: the resulting pages are not “Well-formed XHTML”, and the text input form looks crooked. It is necessary to make the browser receive well-formatted pages, i.e. doctype , html , head , body needed, in doctype , everything is as it should be, and combing the form with css.

We need a page template in which you can insert the result of the view rendering, in rails this is called layout .
All you need to do is add formatting.rhtml to app/views/layouts .

Now let's compile the input form - style.css bit better and put it in public/stylesheets .

That's all for today, the first part is over.



Do not judge strictly, I did not write for a long time in Russian, more and more on Ruby :)

Surely from my muddled description is not a lot clear, looking at the article, I understand that there will be many questions. For example, what form_for or options_for_select . People who are really interested in:



Those who want to see how the text marked up markdown looks can look at the source of the article .

What could be next?

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


All Articles