Ruby-on-rails 如何检查参数是真还是假?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1711547/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:56:43  来源:igfitidea点击:

How to check if a param is true or false?

ruby-on-railsruby

提问by Shpigford

This is really racking my brain, but maybe I'm trying to hard.

这真的让我绞尽脑汁,但也许我正在努力。

I'm passing a param via a URL (example.com?debug=true)

我通过 URL 传递参数 (example.com?debug=true)

So I basically want to say:

所以我基本上想说:

if params[:debug] == true
 do xyz
else
 do abc
end

But for whatever reason that if statement just isn't doing like it seems like it should.

但是无论出于何种原因,if 语句都没有像它应该做的那样。

Is there a better way to do that if/else statement based on a param being true or false?

有没有更好的方法来根据参数为真或假来执行 if/else 语句?

The debug param will either have a value of true, no value, or a value of false (as far as the URL goes).

调试参数的值为真、无值或值为假(就 URL 而言)。

回答by sepp2k

params come in as strings, so you need to compare against "true", not true.

params 以字符串形式出现,因此您需要与 进行比较"true",而不是true

回答by toxaq

You could use ActiveRecord's method of checking truthful values if you don't want to reinvent the wheel (this is what is used when passing params inside an ActiveRecord object

如果您不想重新发明轮子,您可以使用 ActiveRecord 的检查真实值的方法(这是在 ActiveRecord 对象中传递参数时使用的方法)

Rails 3-4.1

导轨 3-4.1

if ActiveRecord::ConnectionAdapters::Column.value_to_boolean(params[:debug])
    do xyz
else
    do abc

Rails 4.2.0

导轨 4.2.0

ActiveRecord::Type::Boolean.new.type_cast_from_database(params[:debug])

Rails 5

导轨 5

ActiveModel::Type::Boolean.new.cast(params[:debug])

Might be worth wrapping in a helper but never the less it's quite flexible:

可能值得包装在一个助手中,但它非常灵活:

rails c
Loading development environment (Rails 3.2.6)
1.9.3p194 :001 > ActiveRecord::ConnectionAdapters::Column.value_to_boolean '1'
 => true 
1.9.3p194 :002 > ActiveRecord::ConnectionAdapters::Column.value_to_boolean '0'
 => false 
1.9.3p194 :003 > ActiveRecord::ConnectionAdapters::Column.value_to_boolean 1
 => true 
1.9.3p194 :004 > ActiveRecord::ConnectionAdapters::Column.value_to_boolean true
 => true 
1.9.3p194 :005 > ActiveRecord::ConnectionAdapters::Column.value_to_boolean 'true'
 => true 
1.9.3p194 :006 > ActiveRecord::ConnectionAdapters::Column.value_to_boolean 'on'
 => true 
1.9.3p194 :007 > ActiveRecord::ConnectionAdapters::Column.value_to_boolean 'off'

Custom extension

自定义扩展

Some people frown on extending core classes but this does fit with the DRY principle.

有些人不赞成扩展核心类,但这确实符合 DRY 原则。

# config/initializer/boolean.rb
class Boolean
  def self.parse(value)
    ActiveRecord::ConnectionAdapters::Column.value_to_boolean(value)
  end
end

Then used like

然后像

if Boolean.parse(params[:debug])
  then xyz

回答by Jake

Since Rails/ActiveRecord 4.2.0 it is

从 Rails/ActiveRecord 4.2.0 开始,它是

if ActiveRecord::Type::Boolean.new.type_cast_from_user params[:debug]
  do xyz
else
  do abc
end

In Rails 5 it is:

在 Rails 5 中,它是:

if ActiveModel::Type::Boolean.new.cast params[:debug]
  do xyz
else
  do abc
end

回答by Ed S.

But for whatever reason that if statement just isn't doing like it seems like it should.

但是无论出于何种原因,if 语句都没有像它应该做的那样。

I can almost guarantee that it is doing exactly what it should. When things don't make sense, one of our assumptions is wrong.

我几乎可以保证它正在做它应该做的事情。当事情没有意义时,我们的假设之一是错误的。

Is the value actually a boolean or is it string (or something else?). If the value is a string then of course the comparison to the booleanvalue true will fail. Try comparing to 'true' and see if that works.

该值实际上是布尔值还是字符串(或其他内容?)。如果该值是一个字符串,那么与布尔值 true的比较当然会失败。尝试与 'true' 进行比较,看看是否有效。