Sorry for the delay – we’ve been on the rails

So it’s been a few months since the last post about adventures in flex.  The final verdict, quite nice but without more developer support the obesity-elab project was too big for one person.  So, we decided to go with the flow and follow the rails path like myExperiment and Sysmo.  This gave us a big codebase to play with.  We still needed some back end components to handle csv file parsing and creation so a jetty based webapp was created using the Ostermiller Utilities for the CSV handling. We (well, me) also used Apache solr with the ruby acts_as_solr plugin for searching the metadata.

Writing complex apps in html is certainly an interesting challenge and after having created the first version of what we now call methodbox I can see why frameworks like Google Web Toolkit and flex are so appealing.  Having to insert hidden objects into the html so the application can remember them between clicks gets a bit tedious after a while! However, we overcame a few hurdles along the way and here are some things I found useful:

Things I needed to know about Rails but was too much of a newby to find out from the books

1. routes.rb.  How to specify routes correctly.  Part of the functionality of methodbox is selecting multiple surveys and searching them.  I couldn’t get the app to return to the correct route after doing the search. Of course, I had not specified the route correctly.  So, if you are returning a collection you need something like this:

map.resources:surveys, :member => {:download => :get}, :collection => {:add_to_pseudo_cart => :get,  :data => :get, :datagrid => :get,:hide_info => :get, :more_info => :get, :search_variables => :get,:sort_variables => :get }  do |survey|

2. Check box tag.  Use check_box_tag to allow you to select multiple objects in the page and send them across inside a hash to the controller.

<%= check_box_tag “entry_ids[]”,,false, :class=>’survey_checkbox’ %>

where item is the object which you want to select or not, false means unchecked when created

3. Selecting, unselecting checkboxes programatically.  Lets say you want a gmail style select all, none (and an ‘invert’ selection – ie. select those not selected and vice versa).  Use link to function and some javascript like this:

<%= link_to_function ‘All’, “$$(‘input.survey_checkbox’).each(function(checkbox) { checkbox.checked = true; });” %>

<%= link_to_function ‘None’, “$$(‘input.survey_checkbox’).each(function(checkbox) { checkbox.checked = false; });” %>

<%= link_to_function ‘Invert selection’, “$$(‘input.survey_checkbox’).each(function(checkbox) { if (checkbox.checked == true) {checkbox.checked = false;} else {checkbox.checked = true; } });” %>

where ‘survey_checkbox’ is the class name of the check_box_tag (see 2 above).

4. XML creation and parsing.  Tried the various libraries but found libxml the fastest.  I needed to parse some fairly big (1 meg+) xml files which contained the metadata for the surveys we are using.  These surveys take the form

<metadata year="2000">
<description>BP Probs: No problems taking blood pressure</description>
<Label>Refusal (8)</Label>
<Label>Don't know (9)</Label>
<MissingValues>-99  thru -1</MissingValues>

Here is some sample code to parse (some of) this using libxml (don’t forget your require ‘xml’ bit):

parser = XML::Parser.file(‘metadata.xml’)
doc = parser.parse

nodes = doc.find(‘//metadata/variable’)

nodes.each do |node|
namenode = node.find(‘child::name’)
namecontent = namenode.first.content = namecontent

descnode = node.find(‘child::description’)
desccontent = descnode.first.content

catnode = node.find(‘child::category’)
catcontent = catnode.first.content

dernode = node.find(‘child::derivation’)
dercontent = dernode.first

dertype = dercontent.find(‘child::type’)
dertypecontent = dertype.first.content

dermethod = dercontent.find(‘child::method’)
dermethodcontent = dermethod.first.content

infonode = node.find(‘child::information’)
infocontent = infonode.first.content


5. gotAPI.  I used this online resource ‘a lot’.  A superb go to when the books are confusing or hard to navigate.

So, a few things which confused me and one major resource you really need.  See you on the Rails.

2 thoughts on “Sorry for the delay – we’ve been on the rails”

  1. For the invert checkbox option, could you not use:

    each(function(checkbox) { checkbox.checked = !checkbox.checked })

    That would be a more obvious inversion. :)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>