I have a silly question. I have a form that is filled out that has a
number of boolean values in it, represented by checkboxes. After the
request is posted, I am generating an e-mail to send to the person
handling the request.
In my mailer view (send_request.text.plain.rhtml), I do something like
Important thing one..: <%= @req.important_thing_1 %>
Important thing two..: <%= @req.important_thing_2 %>
Important thing three: <%= @req.important_thing_3 %>
Which is rendered as
Important thing one..: false
Important thing two..: true
Important thing three: false
I would like false to display as 'No' and true to display as 'Yes'. Is
there a knob or switch somewhere in rails to configure how boolean
values are converted into strings?
I have a silly question. I have a form that is filled out that has a
number of boolean values in it, represented by checkboxes. After the
request is posted, I am generating an e-mail to send to the person
handling the request.
In my mailer view (send_request.text.plain.rhtml), I do something like
Important thing one..: <%= @req.important_thing_1 %>
Important thing two..: <%= @req.important_thing_2 %>
Important thing three: <%= @req.important_thing_3 %>
Which is rendered as
Important thing one..: false
Important thing two..: true
Important thing three: false
I would like false to display as 'No' and true to display as 'Yes'. Is
there a knob or switch somewhere in rails to configure how boolean
values are converted into strings?
I don't know if there is, but you could do something like this (and get
it loaded via environment.rb):
class TrueClass
def to_yesno
'Yes'
end
end
class FalseClass
def to_yesno
'No'
end
end
And then in your views do:
Important thing one..: <%= @req.important_thing_1.to_yesno %>
Important thing two..: <%= @req.important_thing_2.to_yesno %>
Important thing three: <%= @req.important_thing_3.to_yesno %>
that will produce 'Yes' for true and 'No' for false; if you want an
empty string for nil, try:
(bool ? 'Yes' : 'No') unless bool.nil?
that will return nil if bool is nil, which will turn into an empty
string in an erb template (since nil.to_s is '').
Of course, you could wrap that in a helper if you really want to, or
add custom methods to TrueClass, FalseClass and NilClass... but
personally, I find it clean/dry enough just to use the operator.
that will produce 'Yes' for true and 'No' for false; if you
want an empty string for nil, try:
(bool ? 'Yes' : 'No') unless bool.nil?
that will return nil if bool is nil, which will turn into an
empty string in an erb template (since nil.to_s is '').
Of course, you could wrap that in a helper if you really want
to, or add custom methods to TrueClass, FalseClass and
NilClass... but personally, I find it clean/dry enough just
to use the operator.
Hello,
Thanks, that works fine. I normally avoid the ternary operator, but in
this case, it's a very straight forward replacement so I've chosen this
route.