SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2007
    Location
    Texas
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Boolean Question

    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.

  2. #2
    l 0 l silver trophybronze trophy lo0ol's Avatar
    Join Date
    Aug 2002
    Location
    Palo Alto
    Posts
    5,329
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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.

  3. #3
    SitePoint Zealot
    Join Date
    May 2007
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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 ...

  4. #4
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Or like this:

    Code ruby:
    class TrueClass
      def in_english
        "yes"
      end
    end
     
    class FalseClass
      def in_english
        "no"
      end
    end

  5. #5
    SitePoint Enthusiast
    Join Date
    Oct 2007
    Location
    Texas
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks... I will give these a try
    Web design is cool. Roll with it.

  6. #6
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Fenrir2 View Post
    Or like this:

    Code ruby:
    class TrueClass
      def in_english
        "yes"
      end
    end
     
    class FalseClass
      def in_english
        "no"
      end
    end
    I have to say, I don't really like this at all. Surely True in english is just "True"?

    I'd go with the helper method posted earlier. Don't get into the habit of reopening core classes just to provide view transformations.

  7. #7
    SitePoint Enthusiast
    Join Date
    Oct 2007
    Location
    Texas
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, I was actually planning on using the helper because that solution confused me a bit
    Web design is cool. Roll with it.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •