From: "Carlo E. Prelz" Date: 2012-12-14T05:44:43+09:00 Subject: Re: ruby on rails or sinatra for dynamic browser application Subject: ruby on rails or sinatra for dynamic browser application Date: Fri 14 Dec 12 05:21:27AM +0900 Quoting LMH medchem (lists@ruby-forum.com): > 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? 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 -- * Se la Strada e la sua Virtu' non fossero state messe da parte, * K * Carlo E. Prelz - fluido@fluido.as che bisogno ci sarebbe * di parlare tanto di amore e di rettitudine? (Chuang-Tzu)