Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a web page with ruby without rails

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 result in a simple table (for example, a simple log or report). Example:

<?php
$con = mysql_connect("localhost","user","pass");
mysql_select_db("mydb");

$query = mysql_query("SELECT * FROM users");

while($data = mysql_fetch_assoc($query) {
   echo "<p> $data[name] - $data[age] </p>";
}
?>

This is just a simple example, but reflects what I need to do with ruby. I don´t want all rails framework to just do something like this. How can I achieve this with simple ruby code?

like image 810
rizidoro Avatar asked Apr 14 '11 14:04

rizidoro


People also ask

Can you build a website with Ruby?

HOWEVER, Ruby is also a very powerful tool in creating web applications due to it's object-oriented nature of the language, how it is easy to test, and all the open-source frameworks available. Frameworks are packages for different programming languages that can add on many different features and functionality.

Can you use Rails without Ruby?

Without knowing Ruby, it would be hard to take advantage of much of Rails' power. The answer to the first question is most certainly yes. While knowing Ruby first would make learning Rails faster/easier, you can easily learn both at the same time.

Is Ruby on Rails for web development?

Ruby on Rails is a back-end web development framework set on making the time of RoR developers easier by improving development time and functionality.


2 Answers

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.

like image 83
Krule Avatar answered Nov 01 '22 07:11

Krule


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.

like image 31
regularfry Avatar answered Nov 01 '22 07:11

regularfry