Forum: Ruby on Rails Change Bit Value To Text When Viewed

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Eed3ca2591560a2dd91222d9b62f882d?d=identicon&s=25 Ryan Prins (Guest)
on 2006-02-27 02:39
(Received via mailing list)
Hey All, very simple question for you folks ;)

I have a @commutes object that for each record there is a bit value set
(0/1) to denote if an accident occurred. What I want to do is have it
display "Yes" or "No" when I view the listing of commutes. I can get it
to
display the bit value just fine with <%= commute.accident %> but how can
I
get it so that when a record with a 0 comes up that it displays "No" and
if
a 1 "Yes"? Thanks in advance!
455ac2a64d06dc8461f4d258d7f7e980?d=identicon&s=25 Michael Trier (Guest)
on 2006-02-27 02:48
(Received via mailing list)
<%= commute.accident ? "Yes" : "No" %>

Michael
Eed3ca2591560a2dd91222d9b62f882d?d=identicon&s=25 Ryan Prins (Guest)
on 2006-02-27 02:51
(Received via mailing list)
Perfect! That's exactly what I needed.

Ryan
F4405aa9f526aeea20541ca1aa3705e6?d=identicon&s=25 Mathias Stjernström (Guest)
on 2006-02-27 09:13
(Received via mailing list)
Maybe you can try the Acts_as_bitfield plugin

http://wiki.rubyonrails.org/rails/pages/Acts+As+Bi...

Cheers!

--
Mathias Stjernstrom
Ae60e09b991e7a95e44591eb8756d1f6?d=identicon&s=25 Mike D (Guest)
on 2006-02-27 19:04
Michael Trier wrote:
> <%= commute.accident ? "Yes" : "No" %>
>

Following on to this solution: how would I go about it if I wanted to
encapsulate this logic in a method? E.g.:

<%= commute.accident.YesNo %>

Is it possible to define the method in such a way that it is available
for every boolean attribute in my model?

Thanks,
Mike
455ac2a64d06dc8461f4d258d7f7e980?d=identicon&s=25 Michael Trier (Guest)
on 2006-02-27 19:21
(Received via mailing list)
Probably, and in that case you would open up the
ActiveRecord::ConnectionAdapters::Column class and add in the method,
for instance boolean_to_string.  That may or may not be the right spot
for it.

Generally what I see is that you would define a helper, and use that.
For instance, in your helpers/application_helper.rb file put in
something like

def YesNo(value)
  value ? "Yes" : "No"
end

Then in your view you do

<%= YesNo(commute.accident) %>

Now granted, I'm just learning this stuff, so there's probably another
way that is a much better approach.

Michael
Eed3ca2591560a2dd91222d9b62f882d?d=identicon&s=25 Ryan Prins (Guest)
on 2006-02-28 05:53
(Received via mailing list)
Just to clarify:

<%= commute.accident ? "Yes" : "No" %>

Didn't work for me, but the following did.

<%= commute.accident == 1 ? "Yes" : "No" %>

Thanks!
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-02-28 06:03
(Received via mailing list)
I would think that

<%= commute.accident? ? "Yes" : "No" %>

should have also worked

Craig
Ef0db53920b243d6758c2f6b1306df0d?d=identicon&s=25 Steve Ross (cwd)
on 2006-02-28 08:08
You might want to handle true and false different from zero and nonzero.
Rails translates bits to boolean TrueClass and FalseClass, so this code
works for me:

    # Translate boolean true/false to string 'Yes' / 'No' for
human-friendly reading
    # of checkbox fields.
    def handle_translations(field)
    	case field
    	when TrueClass, FalseClass
    		(field) ? 'Yes' : 'No'
    	else
    		field
    	end
    end

Craig White wrote:
> I would think that
>
> <%= commute.accident? ? "Yes" : "No" %>
>
> should have also worked
>
> Craig
This topic is locked and can not be replied to.