Rails(或 Ruby):是/否而不是真/假
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3664181/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rails (or Ruby): Yes/No instead of True/False
提问by aarona
I know I could easily write a function and put it in the application controller, but I'd rather not if there is something else that does this already. Basically I want to have something like:
我知道我可以轻松地编写一个函数并将其放入应用程序控制器中,但是如果已经有其他东西可以做到这一点,我宁愿不这样做。基本上我想有类似的东西:
>> boolean_variable?
=> true
>> boolean_variable?.yesno
=> yes
>> boolean_variable?.yesno.capitalize
=> Yes
is there something like this already in the Rails framework?
Rails 框架中已经有这样的东西了吗?
回答by Neal
There isn't something in Rails.
Rails 中什么都没有。
A better way than adding to the true/false classes to achieve something similar would be to make a method in ApplicationHelper:
比添加到 true/false 类以实现类似的更好的方法是在 ApplicationHelper 中创建一个方法:
def human_boolean(boolean)
boolean ? 'Yes' : 'No'
end
Then, in your view
那么,在你看来
<%= human_boolean(boolean_youre_checking) %>
Adding methods to built-in classes is generally frowned upon. Plus, this approach fits in closely to Rails' helpers like raw().
向内置类添加方法通常是不受欢迎的。另外,这种方法非常适合 Rails 的帮助程序,如raw().
Also, one offs aren't a great idea as they aren't easily maintained (or tested).
此外,一次性不是一个好主意,因为它们不容易维护(或测试)。
回答by Jordan Running
No such built-in helper exists, but it's trivially easy to implement:
不存在这样的内置帮助程序,但它很容易实现:
class TrueClass
def yesno
"Yes"
end
end
class FalseClass
def yesno
"No"
end
end
回答by Mike Heijmans
There is a gem for that now: humanize_boolean
现在有一个宝石:humanize_boolean
Then you just do:
然后你只需要做:
true.humanize # => "Yes"
false.humanize # => "No"
It also supports internationalization so you can easily change the returned string by including your translation for en.boolean.yes and en.boolean.no (or any locale you like)
它还支持国际化,因此您可以通过包含 en.boolean.yes 和 en.boolean.no(或您喜欢的任何语言环境)的翻译轻松更改返回的字符串
回答by the-undefined
The humanize_booleangem extends the TrueClass, FalseClassand NilClasswhich is an indirect extension I would prefer to avoid.
该humanize_boolean宝石扩展TrueClass,FalseClass并且NilClass这是一个间接的扩展我宁愿避免。
I've found this helper with a top level translation is isolated, friendly to change and you can give it anything truthy or falsey:
我发现这个具有顶级翻译的助手是孤立的,易于更改,您可以给它任何真假:
# app/helpers/application_helper.rb
class ApplicationHelper
def humanize_boolean(boolean)
I18n.t((!!boolean).to_s)
end
end
# config/locales/en.yml
en:
:true: 'Yes'
:false: 'No'
Doing !!(boolean).to_sensures the variables passed to the argument is either a "true"or "false"value when building the translation string.
这样做!!(boolean).to_s可确保在构建翻译字符串时传递给参数的变量是 a"true"或"false"value。
In use:
正在使用:
# app/views/chalets/show.html.erb
<%= humanize_boolean(chalet.rentable?) %>
回答by patrickhawley
Alternatively, you could also do one offs in your views such as:
或者,您也可以在您的视图中做一次关闭,例如:
<%= item.bool_field? ? 'yes' : 'no' %>
回答by Ivan Raszl
I love the generalised solutions in the previous answers, but if you want to selectively use a humanised version of your boolean in your views, you could do the following:
我喜欢前面答案中的通用解决方案,但是如果您想在视图中选择性地使用布尔值的人性化版本,您可以执行以下操作:
I put this into helpers/application_helper.rb:
我把它放到 helpers/application_helper.rb 中:
module ApplicationHelper
def humanize_boolean(value)
case value
when true
"Yes"
when false
"No"
when nil
"Undefined"
else
"Invalid"
end
end
end
Then from views you can call it as in this example:
然后从视图中,您可以像在此示例中一样调用它:
Published: <%= humanize_boolean(post.published) %>

