Making a web page with ruby without rails

前端 未结 4 1501
庸人自扰
庸人自扰 2020-12-14 03:38

I´m looking for a way to do simple ruby code without rails. I´m coming from PHP world, and sometimes, I just build a page with a Mysql connection, run a query and show resul

相关标签:
4条回答
  • 2020-12-14 04:21

    The simplest way I know of is to use Ruby's CGI module, and run the script as CGI. Depending on your performance requirements and complexity of the page you want to generate, something like this might just do the trick:

    #!/usr/bin/ruby
    require 'cgi'
    require 'mysql'
    
    con = Mysql.new("localhost","user","pass","mydb")
    rs = con.query('select * from users')
    
    cgi = CGI.new('html4')
    cgi.out {
      cgi.html {
        cgi.body {
          rs.each_hash { |row| puts #{"row['name']} - #{row['age']}" }
        }
      }
    }
    

    You would want to consider the overhead of establishing connection to MySQL on each request, and for complex pages you will need a templating mechanism.

    0 讨论(0)
  • 2020-12-14 04:27

    First, Ruby is not like php. No droping files into public_html and expecting everything to work.

    Never the less, it is possible to do it that way, kinda. So we are using Mysql adapter with no ORM as php does by default.

    Before you start, you will need mysql adapter, so install it with:

    gem install mysql2
    

    Than write something like:

    require "rubygems"
    require "mysql2"
    
    client = Mysql2::Client.new(
      :host => "127.0.0.1",
      :username => "root",
      :password => "",
      :database => "mydb"
    )
    records = client.query("SELECT * FROM users")
    
    records.each {|r| p "<p>#{r['name']} - #{r['age']}</p>"}
    

    Now run it in console with

    ruby name_of_the _file.rb
    

    This will output records in console. If you want browser output, you will have to write a small server:

    #!/usr/bin/ruby
    require 'rubygems'
    require 'socket'
    require 'mysql2'
    
    webserver = TCPServer.new('127.0.0.1', 6789)
    
    client = Mysql2::Client.new(
      :host => "127.0.0.1",
      :username => "root",
      :password => "",
      :database => "mydb"
    )
    
    records = client.query("SELECT * FROM users")
    
    while (session = webserver.accept)
       session.print "HTTP/1.1 200/OK\r\nContent-type:text/html\r\n\r\n"
       request = session.gets
       records.each {|r| session.print "<p>#{r['name']} - #{r['age']}</p>"}
       session.close
     end
    

    Now when you do ruby application.rb, server will be started on port 6789 and it will output required data. You can later reverse proxy on it and use it on port 80.

    0 讨论(0)
  • 2020-12-14 04:32

    The simple answer is sinatra. Or camping.

    However, the slightly longer answer is that Ruby doesn't follow the same execution model as PHP, so the development model of "stick some code in a file to be interpolated by the web server on every request" isn't as well supported.

    0 讨论(0)
  • 2020-12-14 04:33

    This should do the work:

    require 'rubygems'
    gem 'activerecord'; require 'active_record'
    
    class User < ActiveRecord::Base
    
    end
    
    ActiveRecord::Base.establish_connection(
      :adapter => 'mysql',
      :database => 'test',
      :user => 'user',
      :password => 'pass'
    )
    
    User.all.each do |u|
      puts "<p>#{u.name} - #{u.age}</p>"
    end
    

    Before running it you need to install activerecord:

    gem install activerecord
    
    0 讨论(0)
提交回复
热议问题