V
V
Vlad Chernik2016-05-23 22:25:16
ruby
Vlad Chernik, 2016-05-23 22:25:16

Saving data from client to server in Ruby?

Good afternoon!
I ask for advice or, perhaps, even an implementation example, if anyone has one.
The question is quite simple (and probably stupid), but I could not find an answer to it due to the dominance of Rails. No matter how I make a search query, I stumble upon manuals on ActiveRecord and its "friends".
Actually, my question is this: how to transfer data from the client (for example, values ​​from the form filled in by the user) to the server, so that the server already saves the data in the database? Server without Rails but with Ruby.
In simple words: I have form.html with inputs and forms, I have calc.rb with a certain algorithm for calculating the received values ​​and saving them in the database. How can I link these files?
I understand that the question is amateurish, I ask you to forgive me for this.
Thanks in advance for any help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Mirilaczvili, 2016-05-23
@vladchernik

  1. Displaying the form to the client, GET
    1. Installing the minimalistic framework SinatraRb
    2. Gemfile in the root of the project
    source 'https://rubygems.org'
    
    gem 'rack'
    gem 'sinatra'
    #gem 'haml'
    #gem 'sqlite3'

    and execute the command on the command line
    (gem bundler - gem dependency management - must be installed in advance)
    3. The config.ru file in the root of the project
    # config.ru (run with rackup)
    require 'rubygems'
    require 'bundler/setup'
    
    Bundler.require
    
    require './myapp.rb'
    run Sinatra::Application

    4. The file myapp.rb in the root of the project
    require 'sinatra'
    
    get '/' do
      erb :index # Отобразить форму по пути views/index.erb. Erb - это код HTML с вкраплениями кода на Ruby
    end
    
    post '/do_it'
      "Thank you, #{params['name']}!"
    end

    5.views/index.erb
    <form action="/do_it" method="post">
        <div>
            <label for="name">Your Name:</label><br>
            <input type="text" name="name"><br>
        </div>
        <button type="submit">Submit</button>
    </form>

    Additionally
    Accessing the Request Object
    To work with the DBMS, there is also a simple DataMapper gem

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question