如何在Rails中的某些情况下实现是/否而不是布尔值?

时间:2022-08-04 10:00:44

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.

我是编程的新手,但在我的应用程序中,我希望某些情况显示“是”或“否”而不是“真”或“假”。我不确定这样做的最好方法,我读了这个问题,但并不真正理解如何实现它。有人可以帮助我,最好把它放在初始化器,帮助器或其他地方吗?我希望能够在我的视图中调用某些内容,无论我想要显示是/否,或者创建自定义数据类型,在我的migation中,我可以创建类似t.boolean_yesno的内容,然后为每个列创建对它来说,它只会将真实存储为是,而将虚假存储为否定。

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

我很感激帮助我走上正轨,我没有初始化者或助手的经验。谢谢!

3 个解决方案

#1


26  

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.

我建议使用Rails语言环境。这些使您可以为任何变量定义语言文本。例如,您的应用可以为英语使用者说​​“是”/“否”,为法语使用者说​​“Oui”/“Non”。

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.

语言环境也快速,灵活且易于更改,因为它们独立于典型的源代码。您可以看到语言环境如何在源代码逻辑与您呈现的文本之间实现非常好的分离。

Example:

#./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] %>

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

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

Monkey Patching

You asked about this question: Rails (or Ruby): Yes/No instead of True/False.

你问过这个问题:Rails(或Ruby):Yes / No而不是True / False。

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

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

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).

这被称为“猴子修补”Ruby类。一般来说,做你想做的事并不是一个好主意。为什么不?因为它打破了现有的Ruby类来强制该方法进入所有代码。这在极少数情况下可能没问题(恕我直言),但绝对不适用于将视图文本修补为逻辑类(再次恕我直言)。

How about a migration data type?

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.

您有关于创建自己的迁移数据类型的正确概念,但对于您的情况可能有点过分,因为布尔值是对于是/否的典型一对一匹配。您想什么时候创建自己的类型?例如,如果要使用不同的数据库原始类型存储yes / no,例如在一个数据库中使用位标志而在另一个数据库中使用字符串枚举。

Decorators

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.

Rails语言环境本质上是装饰器。

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.

对于更高级的需求,有一个很好的装饰宝石叫做“Draper”,很容易使用。使用装饰器往往有助于查看代码组织良好,命名空间,并且更容易测试。

#2


3  

The simplest option to get setup is to create a helper method which you can put in the application helper.

获取设置的最简单选项是创建一个辅助方法,您可以将其放入应用程序帮助程序中。

# in app/helpers/application_helper.rb

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

This is analogous to many other Rails conversion helpers such as number_to_currency

这类似于许多其他Rails转换助手,例如number_to_currency

#3


0  

Assuming you have :boolean attribute in your model/database, you can do this:

假设你的模型/数据库中有:boolean属性,你可以这样做:

class Foo < ActiveRecord::Base
  def bar
    self[:bar] ? 'Yes' : 'No'
  end
end

The whenever you want the "Yes"/"No" value, call @foo.bar. Whenever you want the true/false value, call @foo.bar? or @foo[:bar].

只要您想要“是”/“否”值,请致电@ foo.bar。每当你想要真/假值时,请调用@ foo.bar?或@foo [:bar]。

#1


26  

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.

我建议使用Rails语言环境。这些使您可以为任何变量定义语言文本。例如,您的应用可以为英语使用者说​​“是”/“否”,为法语使用者说​​“Oui”/“Non”。

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.

语言环境也快速,灵活且易于更改,因为它们独立于典型的源代码。您可以看到语言环境如何在源代码逻辑与您呈现的文本之间实现非常好的分离。

Example:

#./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] %>

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

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

Monkey Patching

You asked about this question: Rails (or Ruby): Yes/No instead of True/False.

你问过这个问题:Rails(或Ruby):Yes / No而不是True / False。

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

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

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).

这被称为“猴子修补”Ruby类。一般来说,做你想做的事并不是一个好主意。为什么不?因为它打破了现有的Ruby类来强制该方法进入所有代码。这在极少数情况下可能没问题(恕我直言),但绝对不适用于将视图文本修补为逻辑类(再次恕我直言)。

How about a migration data type?

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.

您有关于创建自己的迁移数据类型的正确概念,但对于您的情况可能有点过分,因为布尔值是对于是/否的典型一对一匹配。您想什么时候创建自己的类型?例如,如果要使用不同的数据库原始类型存储yes / no,例如在一个数据库中使用位标志而在另一个数据库中使用字符串枚举。

Decorators

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.

Rails语言环境本质上是装饰器。

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.

对于更高级的需求,有一个很好的装饰宝石叫做“Draper”,很容易使用。使用装饰器往往有助于查看代码组织良好,命名空间,并且更容易测试。

#2


3  

The simplest option to get setup is to create a helper method which you can put in the application helper.

获取设置的最简单选项是创建一个辅助方法,您可以将其放入应用程序帮助程序中。

# in app/helpers/application_helper.rb

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

This is analogous to many other Rails conversion helpers such as number_to_currency

这类似于许多其他Rails转换助手,例如number_to_currency

#3


0  

Assuming you have :boolean attribute in your model/database, you can do this:

假设你的模型/数据库中有:boolean属性,你可以这样做:

class Foo < ActiveRecord::Base
  def bar
    self[:bar] ? 'Yes' : 'No'
  end
end

The whenever you want the "Yes"/"No" value, call @foo.bar. Whenever you want the true/false value, call @foo.bar? or @foo[:bar].

只要您想要“是”/“否”值,请致电@ foo.bar。每当你想要真/假值时,请调用@ foo.bar?或@foo [:bar]。