Database Reference
In-Depth Information
The output looks like this:
<P align= "left" > This is a sentence. </P>
To display generated HTML content, pass it in a code block to the cgi.out method. The
following Ruby script, show_tables.rb , retrieves a list of tables in the cookbook database
and displays them as an HTML document:
#!/usr/bin/ruby -w
# show_tables.rb: Display names of tables in cookbook database
require "cgi"
require "Cookbook"
# Connect to database, generate table list, disconnect
dbh = Cookbook . connect
stmt = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'cookbook' ORDER BY TABLE_NAME"
rows = dbh . select_all ( stmt )
dbh . disconnect
cgi = CGI . new ( "html4" )
cgi . out {
cgi . html {
cgi . head {
cgi . title { "Tables in cookbook Database" }
} +
cgi . body () {
cgi . p { "Tables in cookbook Database:" } +
rows . collect { | row | row [ 0 ] + cgi . br } . join
}
}
}
The collect method iterates through the row array containing the table names and
produces a new array containing each name with a <br> appended to it. The join
method concatenates the strings in the resulting array.
The script includes no explicit code for producing the Content-Type: header because
cgi.out generates one.
Install the script in your cgi-bin directory and request it from your browser as follows:
http://localhost/cgi-bin/show_tables.rb
If you invoke Ruby web scripts from the command line so that you can examine the
generated HTML, you'll see that the HTML is all on one line and is difficult to read. To
make the output easier to understand, process it through the CGI.pretty utility method,
which adds line breaks and indentation. Suppose that your page output call looks like
this:
Search WWH ::




Custom Search