I have several boolean types and I would like them to show up saying 'yes' or 'no' in view mode instead of 'true' or 'false'
Does anyone know how to do this?
Thanks,
Matt
| SitePoint Sponsor |
I have several boolean types and I would like them to show up saying 'yes' or 'no' in view mode instead of 'true' or 'false'
Does anyone know how to do this?
Thanks,
Matt
Web design is cool. Roll with it.
You could do it a number of ways. Simple, straightforward way is to just do some variant of an if/else:
Code Ruby:<% if your_boolean_value %> Yes. <% else %> No. <% end %>
Albeit it's a bit lengthy and can muddle up your views. In any language I'm a fan of the ternary:
Code Ruby:<%= your_boolean_value ? "Yes" : "No" %>
You could also do it a number of other ways- via some conditionals in your SQL to return yes/no, via virtual attributes in your model file so you could write something like Model.boolean_word_display or something similar, or so on. Plenty of options to explore.![]()
.
Zach Holman
good-tutorials — blog — twitter — last.fm

you can use a helper
Code Ruby:def boolean_to_meaning(value) case value when true : "yes" when false : "no" end end
So every time you need to convert boolean to yes/no you have just to call it
Alla prossima ...




Or like this:
Code ruby:class TrueClass def in_english "yes" end end class FalseClass def in_english "no" end end
Thanks... I will give these a try![]()
Web design is cool. Roll with it.
Thanks, I was actually planning on using the helper because that solution confused me a bit![]()
Web design is cool. Roll with it.
Bookmarks