Good afternoon, dear community!

I do not know how to correctly formulate my question. I've been trying in vain to find the answer to my question for several days now, but everywhere I come across Rails tutorials. But I don't want to use rails or any other frameworks. Surely my task can be solved without using them.

I have, for example, the index.html file:

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> </head> <body> <form> <label for="total"> Укажите сумму: </label> <input type="text" name="total" id="total" required="required"> <input type="submit" value="Сохранить"> </form> </body> </html> 

And there is, for example, the file main.rb:

 require "sqlite3" db = SQLite3::Database.open "salary.sqlite" total = gets.chomp db.execute "INSERT INTO salary (total) VALUES (?)", [total] db.close if total >= 1 puts "Всё ОК!" else puts "Что-то пошло не так..." end 

Tell me, please, how can I transfer the value of the attribute that the user fills in the form in index.html to the main.rb ruby ​​file so that he, in turn, saves the data to the database?

Many thanks in advance for your help!

  • Sinatra do you also consider a framework? - D-side
  • Well yes. Little dsl framework, isn't it? - vladchernik
  • Well yes. The question is asked for a reason, the definitions of frameworks are for the most part so otfonarnye that you can call the entire Ruby. And what's the use of such definitions? :) - D-side
  • Yes, there is such a thing :) In my question I wanted to say that I would like to execute the request with a minimum of magic in order to understand how it works from the inside. - vladchernik
  • Even this has different levels. Want to listen to a raw TCP socket and independently parse HTTP headers? Or is it enough for you Rack, which translates HTTP request data into Ruby data structures? - D-side

1 answer 1

There are several ways.

  1. CGI .

The method is especially popular for php. The essence of the method is that the web server (Apache or Nginx) will simply call the necessary script. The answer must be placed on the standard puts stream. Here is an article in which there is an example of creating cgi scripts in ruby.

But you need to understand that without a web server "will not fly." According to the settings of the web server can not tell. In addition, the process will be "born to die." Those. it will be necessary to take care that with each request everything is initialized anew (configs, connection to the database, etc.)

  1. Write a Rack-compatible app. And use a rack server like puma or unicorn .

This is more difficult. Although not much - just need to implement an application structure that satisfies the Rack interface. But, as a result, there will be a multi-threaded backend server that can be used both independently and in conjunction with web servers using Apache and Nginx (for selling, it is recommended to do this).

  1. Use native ruby WebRick .

IMHO, the most dreary and most unreliable way. This component has a specific interface (there are examples from the link above) and it did not work well with competing queries. Of course, it is devoid of some of the disadvantages of CGI, but not as effective as the Rack + Backend server. Plus, making the Rack interface much easier.

  • Although I have not received an answer to all the questions that interest me, I find your comment very useful. Thanks for the help! - vladchernik