Ruby on rails or sinatra for dynamic browser application

I need to write a simple html file or web application that will allow me
to display the contents of a text file in a browser. This will involve
some formatting and eventually some dynamic features like sorting. This
is tabular data, so I am more or less creating a spreadsheet display in
a browser. I don’t know how to display a variable amount of data in
html. I can hard code things, but for this application, the number of
rows and columns will be variable.

I want to use ruby because I am using ruby to access a sqlite database
from which the text file is created. Eventually the text file will go
away and the application will just show data that is queried from a
database. It makes sense to use ruby for the dynamic code on the webpabe
if I am using it to access the database. I know that both ruby on rails
and sinatra are used to create web pages, but I don’t know much more
than that.

Can someone point me to a tutorial or example of how to do something
similar? Basically the application needs to open a text file (possibly a
browse button on the webpage to give it a path), read the file into
variables, and then use the variables to populate rows in a display.

Am I on the right track here?

LMHmedchem

Subject: ruby on rails or sinatra for dynamic browser application
Date: Fri 14 Dec 12 05:21:27AM +0900

Quoting LMH medchem ([email protected]):

away and the application will just show data that is queried from a
Am I on the right track here?
If I were you, I would not pull in rails for a simple thing -
especially if you do not plan to have many queries. Webrick (the
pure-ruby web server included in the Ruby source) works
very well.

Remember that you do not need to serve HTML from a web server: simple
text files may be served as well.

Here is a short example about how you may serve text files:

–8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<–
require ‘webrick’
require ‘webrick/httpserver’

class Webserver < WEBrick::HTTPServer
PORT=12345

def initialize
super(:Port=>PORT)
mount_proc(’/’,method(:content).to_proc())
end

def content(req,res)
res[‘content-type’]=‘text/plain’
res.body= <<-EOF
This is
your content
EOF
res.status=WEBrick::HTTPStatus::RC_OK
end
end

Webserver::new().start()
–8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<–

Basically, whenever you access port 12345 of your computer,
method ‘content’ is called, which here returns a hardcoded text - but
the text could be whatever.

Carlo

LMH medchem wrote in post #1089022:

I don’t know how to display a variable amount of data in
html.

data1 = [1, 2, 3]
data2 = [‘a’, ‘b’]

data1.each do |item|
puts “

#{item}

end

data2.each do |item|
puts “

#{item}

end

–output:–

1
2
3
a
b

Carlo E. Prelz wrote in post #1089024:

If I were you, I would not pull in rails for a simple thing -
especially if you do not plan to have many queries. Webrick (the
pure-ruby web server included in the Ruby source) works
very well.

I would recommend Sinatra instead, because it sits on top of a number of
webservers (including Webrick), making it more flexible, and it hides
the low-level detail.

However, start by writing a program which opens your file, iterates
through the lines and writes it to stdout (i.e. forget about the web
part). This should be easy enough if you’re already using ruby.

Then to convert it to sinatra, you just need to append your results to a
string instead.

require ‘sinatra’
get ‘/’ do
res = “”
res << “

\n”
res << “\n”
res << “
Hello
\n”
res
end

If you want to continue using “puts” you can even use StringIO:

require ‘sinatra’
require ‘stringio’
get ‘/’ do
r = StringIO.new
r.puts “


r.puts “”
r.puts “
Hello

r.string
end

Then if you want to use a query string to control the output (e.g. to
select which columns to sort by), look at the Sinatra docs for params.

http://www.sinatrarb.com/intro