Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
CGI tables in Ruby

Posted by Milo (Milo), 1 June 2004
I've been trying to convert a script to run on a web server, and discovered that Ruby cgi scripts are as much of the devil as Perl ones. Any suggestions on how to make one thing work would be welcome.
The script normally produces an html table, as follows:

puts "<table border = 1>\n"

puts "<tr><td>switch summary info.</td><td> query id</td><td> subject id</td><td>percent identity</td><td>alignment length</td><td>mis-matches</td><td>gap openings</td><td>query start</td><td>query end</td><td>subject start</td><td>subject end</td><td>e-value</td><td>bit score</td></tr>\n"

switch_summary.each_value do |val|
               line = val.split(",").to_a
               print "<tr>"
               line.each do |l|
                                 print "<td>#{l}</td>"
               end
               print "</tr>\n"
end
puts "</table>"

There may well be other faults with this code, but the main problem I have is converting it to use CGI. Can anyone suggest a rough way to do it?
Thanks!

Posted by admin (Graham Ellis), 1 June 2004
Is this just a snippet of code or the whole thing?   If it's the whole thing, then starting it off with
     #!/usr/local/bin/ruby
     puts "Content-type: text/html\n\n"
should work wonders (you may need to change the ruby location.

If, as I suspect, there's more to the question than that, can you let me know.  Thanks!

Posted by Milo (Milo), 1 June 2004
You are indeed correct - this is a small fragment of a script that I have placed here:
http://www.genomics.ceh.ac.uk/~milo/switchfinder_general/
As many users won't have Ruby, won't want to install it, and will only want to examine one organism I plan to run the code as a CGI script allowing them to select their favourite organism (the same server runs a database with all the data already available) and view the output as a dynamically generated HTML table.
Currently, getting the output into that format is proving rather difficult.
This is my first attempt at any Ruby so it will look dodgy, but at least it works.

Posted by Milo (Milo), 2 June 2004
Graham,
I've now found the solution - I was not aware of the difference between array.each and array.map, so the following code does the trick:


cgi.h3 { "Results for #{genome}"} +
     cgi.table('BORDER' => '1') {
           switch_summary.map { |val|
                 cgi.tr {
                       val.split(",").map { |l| cgi.td {"#{l}"} }
                 } + "\n"
           }
     }

Thanks,
Milo.



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho