I’m planning to write a little CGI to manage a
small database.
What’s currently the best Ruby tool for this
purpose?
My experience is limited to cgi.rb for the
most part.
I don’t think Borges is an option.
The server is Apache, and I’ll be using a
web provider (i.e., no root access to box).
The app is basically to associate keywords
with images. There is a fixed set of keywords.
Display a bunch of images on the left and a
bunch of checkboxes on the right:
(pic1) first second third
(pic2) first second third
(pic3) first second third
The boxes will be initialized based on the
database. The user checks/unchecks any or
all of them, then presses Save. The database
is then updated.
Since I’ll occasionally add pix to the database,
I’ll probably generate the HTML on the fly (i.e.,
to facilitate adding keywords to a new set of
images).
Database choice is not a big deal. I’ll likely
just use Marshal or PStore or DBM or something
equally silly. No one will access this but me,
so it will be strictly single-user.
Come on, guys. You have to write a CGI in Ruby. You don’t
have root access on the box. The server is Apache.
What tool do you use?
Blogtari! :)
Seriously, one of my goals with Blogtari [0] was to keep installation simple and root-free. Most of the hosting services I’ve used
don’t give me root, though I’ve been allowed to compile a local install of Ruby. So, I’ve been tending to write drop-in-place CGI
apps that do not presume any special access or even a shell account. Just file uploading and the ability to change file
permissions.
Of course, Blogtari is not a general purpose framework; it’s a simple blogging app, but it avoids any special installation.
Actually, it avoids using the CGI lib, too, just grabbing some things from ENV. This makes local, serverless development easier.
It doesn’t provide for any file upload or handy web-based blog editing; I use MS Word to edit and post to my home site, using a
combo of VBA and Ruby shell scripts. But when I get around to allowing file uploads, such as for adding images, I’ll likely roll my
own code to grab the ENV values and process file types, etc. I don’t want to have to rely on anything that requires a telnet or ssh
session to install. (But then again I haven’t really looked at what’s in RAA for this.)
An early version of ruby-xml.com used PostgreSQL with DBI, and it was fairly simple to code. I changed this when I switched the
code over to Blogtari and added a simple templating system [1] based on REXML stream parsing. The DB was overkill; it was easier to
use disk files and RSS feeds to populate the templates. (I did have a simple web form for adding content to rubyxml.com by
inserting rows into a PostgreSQL table, but nothing too fancy. DBI is nice, though, and it’s easy to have it emit XML and pass the
XML to a templating tool.)
Provided you can run a CGI, Borges should work. If you’re not using
mod_ruby, you’ll have to figure out how to grab the GET and POST args
yourself, because I’ve not puzzled that out yet.
i have. i’ll put it down here, for you and to make sure i actually know
what i’m doing (check my sanity fellow Rubyists )
CGI data is stored in environment variables passed to the CGI
executable. use Ruby’s global ENV Hash to retrieve them.
ENV[‘REQUEST_METHOD’] is GET or POST. for GET requests,
ENV[‘QUERY_STRING’] is the string after the question mark in the URL.
for example: cgi.rb?key=val;foo=bar
QUERY_STRING = ‘key=val;foo=bar’
split QUERY_STRING on ‘;’ or ‘&’. i use semicolons because it doesn’t
affect URL encoding routines like an ampersand might, but my scripts
check for & just in case. they’re both valid as far as i know. then
split each element on ‘=’ to get your keys and values.
a very simple GET query parser could look like this:
CGI GET query parser
params = {}
ENV[‘QUERY_STRING’].split(/[;&]/).each { |pair|
key, val = pair.split(/=/)
params[key] = val
}
end
note that this simple parser will overwrite repeated keys, which is not
standards compliant. Ruby’s cgi.rb always puts them in an Array to be
safe (params[key].push val). ask me or see Perl5’s CGI.pl for another
way to deal with it. (my way is Perl5’s in Ruby )
extra: PATH_INFO is also filled in, if the script name is followed by
a / and some text in the request URL. PATH_TRANSLATED is PATH_INFO
appended to the httpd’s DocumentRoot. following the path with an
optional question mark can create a QUERY_STRING as well as the
PATH_INFO.
for example: cgi.rb/foo/bar?key=val
PATH_INFO = ‘/foo/bar’
QUERY_STRING = ‘key=val’
i haven’t done any POST work yet, but i believe it’s mostly the same,
with some checking of CONTENT_LENGTH and such to ensure you have the
data you want.
On Sunday, March 16, 2003, at 07:08 , Eric Hodel wrote:
Provided you can run a CGI, Borges should work. If you’re not using
mod_ruby, you’ll have to figure out how to grab the GET and POST args
yourself, because I’ve not puzzled that out yet.
i haven’t done any POST work yet, but i believe it’s mostly the same,
with some checking of CONTENT_LENGTH and such to ensure you have the
data you want.
If the request method is post, it depends on the content-type.
When application/x-www-urlencoded' (or alike), it is parsed just like the query string. When multipart/form-data’ (or alike), things get harder.
You’d best look at cgi.rb or some other parsing mechanism (for
example that one of PHP) to understand how to deal with the
boundaries.
But then you are rewriting the CGI module from scratch…!
You can instead just subclass CGI and provide it with methods to fetch the
environment (envtable) and POST data (stdinput), and it will do all the work
for you.
On Tue, Mar 18, 2003 at 02:22:47PM +0900, Justin White wrote:
On Sunday, March 16, 2003, at 07:08 , Eric Hodel wrote:
Provided you can run a CGI, Borges should work. If you’re not using
mod_ruby, you’ll have to figure out how to grab the GET and POST args
yourself, because I’ve not puzzled that out yet.
i have. i’ll put it down here, for you and to make sure i actually know
what i’m doing (check my sanity fellow Rubyists )
CGI data is stored in environment variables passed to the CGI
executable. use Ruby’s global ENV Hash to retrieve them.
ENV[‘REQUEST_METHOD’] is GET or POST. for GET requests,
ENV[‘QUERY_STRING’] is the string after the question mark in the URL.
for example: cgi.rb?key=val;foo=bar
QUERY_STRING = ‘key=val;foo=bar’