如何实现是/否,而不是布尔在Rails的某些情况下? [英] How to implement Yes/No instead of Boolean for certain cases in Rails?

查看:193
本文介绍了如何实现是/否,而不是布尔在Rails的某些情况下?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个福利局在编程,但是在我的应用我想某些情况下显示是或否,而不是真或假。我不知道这样做的最佳方式,我读<一个href=\"http://stackoverflow.com/questions/3664181/rails-or-ruby-yes-no-instead-of-true-false\">this的问题,但并没有真正理解如何实现它。有人能帮助我,这将是最好把这个在​​初始化,佣工,或别的地方?我希望能够调用东西在我的意见的地方我想显示的是/否,或者创建自定义的数据类型,其中在我migation我可以创造这样t.boolean_yesno,然后为每一列我做的,要它只是存储trues为yes和falses为no。

I'm a newb at programming but in my application I want certain cases to display "yes" or "no" instead of "true" or "false". I'm not sure the best way to do this, I read this question but didn't really understand how to implement it. Can someone help me, would it be best to put this in the initializers, helpers, or somewhere else? I want to be able to call something in my views wherever I want the Yes/No to be displayed, or alternatively to create a custom data type where in my migation I can just create something like t.boolean_yesno and then for each column I do that to it will just store trues as yes and falses as no.

我倒是AP preciate一只手让我在正确的轨道上,我有一个初始化或助手的经验。谢谢!

I'd appreciate a hand in getting me on the right track, I have no experience with initializers or helpers. Thanks!

推荐答案

我推荐使用Rails的语言环境。这使您可以定义语言文本您的任何变量。例如,你的应用程序可以说是/否讲英语,和OUI/非法国扬声器。

Locales

I recommend using Rails locales. These enable you to define language text for any of your variables. For example, your app can say "Yes"/"No" for English speakers, and "Oui"/"Non" for French speakers.

语言环境也快,灵活,容易改变,因为它们是独立的典型来源$ C ​​$ C的。你可以看到的区域设置将如何导致源$ C ​​$ C逻辑与你呈现文本之间很好的分离。

Locales are also fast, flexible, and easy to change because they are independent of your typical source code. You can see how locales will lead to very good separation between your source code logic versus the text that you render.

例如:

#./config/locales/en.yml
en:
  TrueClass: "Yes"
  FalseClass: "No"

#./app/views/items/index.html.erb
The value is <%= translate(myboolean.class) %>
The translate method can be abbreviated like this <%= t myboolean.class %>

助手

您可能会看到其他人使用一个辅助编码这样的:

Helpers

You will likely see other people coding it like this using a helper:

#./app/helpers/application.rb
def yesno(x)
  x ? "Yes" : "No"
end

# ./app/views/items/index.html.erb
<%= yesno(myboolean) %>

或者这样使用常数:

Or like this using a constant:

#./app/helpers/application.rb
YESNO = {true: "Yes", false: "No"}

# ./app/views/items/index.html.erb
<%= YESNO[myboolean] %>

这些都是快速和肮脏的PHP类的解决方案。有更好的方法来做到这一点。

These are both quick-and-dirty PHP-like solutions. There are better ways to do it.

您问这个问题:铁轨(或Ruby) :是/否,而不是真/假

# ./app/lib/extensions/true_class.rb
class TrueClass
  def yesno
   "Yes"
  end
end

# ./app/views/items/index.html.erb
<%= myboolean.yesno %>

这被称为猴子补丁Ruby类。一般来说它不适合做你想要做一个好主意。为什么不?因为它打破了现有的Ruby类,迫使该方法将所有code的。这可能是猴子修补鉴于文本在少数情况下(恕我直言)确定,但肯定不会成为一个逻辑类(再次恕我直言)。

This is called "monkey patching" a Ruby class. In general it's not a good idea for doing what you're trying to do. Why not? Because it breaks an existing Ruby class to force that method into all of your code. This may be OK in rare cases (IMHO) but definitely not for monkey patching view text into a logic class (again IMHO).

您有关于创建您自己的迁移数据类型正确的总体思路,但它可能是矫枉过正的情况下,因为一个布尔值,就是这样一个典型的一对一匹配的是/否。你什么时候想创建自己的类型?例如,如果你想存储是/否使用不同的数据库基本类型,比如在一个数据库中对在另一个数据库字符串枚举一个使用一个位标志。

You have the right general idea about creating your own migration data type, yet it may be overkill for your case because a boolean is such a typical one-to-one match for a yes/no. When would you want to create your own type? For example if you wanted to store the yes/no using different database primitive types, such as a using a bit flag in one database vs. a string enum in another database.

一个装饰为任何应用程序的一般概念。在Rails中,一个装饰就是视图逻辑添加到现有的模型或类的方法。

A decorator is a general concept for any app. In Rails, a decorator is a way to add view logic to an existing model or class.

Rails的语言环境基本上是装饰。

The Rails locales are essentially decorators.

有关更高级的需求,也就是所谓的德雷珀一个很好的装饰宝石这是很容易使用。使用装饰倾向于帮助查看code良好的组织,以及命名空间中,并且更易于测试。

For more advanced needs, there's a good decorator gem called "Draper" which is easy to use. Using decorators tends to help view code be well organized, well namespaced, and easier to test.

这篇关于如何实现是/否,而不是布尔在Rails的某些情况下?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆