Amazon.com Widgets
...not so private reflections of greg.newman
RSS feed - Categories & Search

Simple but True

This is so simple it's almost not even worth posting, but, oh well.

I needed to convert true or false fields from the database to image icons in my views on mutliple pages today, so I wrote this tiny helper and dropped it into my application_helper.rb file.

def true_false(test)<br /> &#xA0;&#xA0;if test == true<br /> &#xA0;&#xA0;&#xA0;&#xA0;icon = 'check.gif'<br /> &#xA0;&#xA0;else<br /> &#xA0;&#xA0;&#xA0;&#xA0;icon = 'x.gif'<br /> &#xA0;&#xA0;end<br /> &#xA0;&#xA0;image_tag(icon)<br /> end

All it does is convert my tinyint fields from MySQL to image icons for a checkmark or an X and I can call it from anywhere since it's in my application helper.

Cheers

0 TrackBacks

Listed below are links to blogs that reference this entry: Simple but True.

TrackBack URL for this entry: http://www.20seven.org/cgi-bin/mt/mt-tb.cgi/43

3 Comments

This is so simple it's almost not even worth posting, but, oh well.

I needed to convert true or false fields from the database to image icons in my views on mutliple pages today, so I wrote this tiny helper and dropped it into my application_helper.rb file.

def true_false(test)
  if test == true
    icon = 'check.gif'
  else
    icon = 'x.gif'
  end
  image_tag(icon)
end

All it does is convert my tinyint fields from MySQL to image icons for a checkmark or an X and I can call it from anywhere since it's in my application helper.

Cheers

Good stuff. I recently did something similar. It's purely a stylistic preference, but you can shorten the method to:

def true_false(test)
  image_tag(test ? 'check.gif' : 'x.gif')
end

Ok. This must be an omen for me to change my style to the old C style if statement formats. (Someone else posted something similar about some php code the other day.

Anyhow... Glad you like, thanks for the hint back.

Leave a comment