How to output a boolean true or false to display yes or no in Ruby on Rails
So you have a boolean check in your database and you want to convert the output from saying "true" or "false" to something more readable like "yes" or "no". It's as easy as:
<%= article.show? ? 'yes' : 'no' %>
Where article is the object and show is the field in my database with the boolean 1 or 0 stored.
About
Paul is a web developer for Kyanmedia web agency. He's lucky enough to write in Ruby on Rails full-time and uses this site to post snippets of code.
Contact
my name at gmail.com
More snippets
Take a look in the archive
Need a website?
Contact my employer. Make sure to check out our portfolio of work.
Hosting
I recommend hostingrails.com
3 comments made
Please, con you check the article? the code is not avaiable.
Hi Pepe,
The code is there, it reads:
article.show? ? 'yes' : 'no'Can you see the code in the html source? Perhaps there is a css bug here? What browser are you using?
Thanks, Paul.
Thanks so much! I was going crazy trying to figure this out. Sometimes the API and I are speaking different languages…
It worked perfectly in my application.
Got something to say?