Ruby-on-rails 如何测试rails中是否存在参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5629402/
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
How to test if parameters exist in rails
提问by Darren
I'm using an IF statement in Ruby on Rails to try and test if request parameters are set. Regardless of whether or not both parameters are set, the first part of the following if block gets triggered. How can I make this part ONLY get triggered if both params[:one] and params[:two] is set?
我在 Ruby on Rails 中使用 IF 语句来尝试测试是否设置了请求参数。无论是否设置了两个参数,都会触发以下 if 块的第一部分。如果 params[:one] 和 params[:two] 都设置了,我怎样才能让这部分只被触发?
if (defined? params[:one]) && (defined? params[:two])
... do something ...
elsif (defined? params[:one])
... do something ...
end
回答by mu is too short
You want has_key?:
你想要has_key?:
if(params.has_key?(:one) && params.has_key?(:two))
Just checking if(params[:one])will get fooled by a "there but nil" and "there but false" value and you're asking about existence. You might need to differentiate:
只是检查if(params[:one])会被“有但为零”和“有但假”的值所迷惑,并且您正在询问存在。您可能需要区分:
- Not there at all.
- There but
nil. - There but
false. - There but an empty string.
- 根本不存在。
- 那里但是
nil。 - 那里但是
false。 - 只有一个空字符串。
as well. Hard to say without more details of your precise situation.
以及。如果没有关于您的确切情况的更多细节,就很难说。
回答by netricate
I am a fan of
我是粉丝
params[:one].present?
params[:one].present?
Just because it keeps the params[sym]form so it's easier to read.
只是因为它保留了params[sym]表格,所以更容易阅读。
回答by Orlando
use blank? http://api.rubyonrails.org/classes/Object.html#method-i-blank-3F
使用空白?http://api.rubyonrails.org/classes/Object.html#method-i-blank-3F
unless params[:one].blank? && params[:two].blank?
will return true if its empty or nil
如果为空或 nil 将返回 true
also... that will not work if you are testing boolean values.. since
也......如果你正在测试布尔值,那将不起作用......因为
>> false.blank?
=> true
in that case you could use
在这种情况下,你可以使用
unless params[:one].to_s.blank? && params[:two].to_s.blank?
回答by Zack Xu
You can write it more succinctly like the following:
您可以像下面这样更简洁地编写它:
required = [:one, :two, :three]
if required.all? {|k| params.has_key? k}
# here you know params has all the keys defined in required array
else
...
end
回答by Jacob Relkin
Simple as pie:
简单如馅饼:
if !params[:one].nil? and !params[:two].nil?
#do something...
elsif !params[:one].nil?
#do something else...
elsif !params[:two].nil?
#do something extraordinary...
end
回答by fl00r
if params[:one] && params[:two]
... do something ...
elsif params[:one]
... do something ...
end
回答by chrpes
A very simple way to provide default values to your params: params[:foo] ||= 'default value'
为参数提供默认值的一种非常简单的方法: params[:foo] ||= 'default value'
回答by Basil Mariano
I just read this on RubyInRails classes http://api.rubyonrails.org/classes/Object.html#method-i-blank-3F
我刚刚在 RubyInRails 类http://api.rubyonrails.org/classes/Object.html#method-i-blank-3F上阅读了这个
you can use blank?method which is equivalent to params[:one].nil? || params[:one].empty?
您可以使用blank?等效于的方法params[:one].nil? || params[:one].empty?
(e.g)
(例如)
if params[:one].blank?
# do something if not exist
else
# do something if exist
end
回答by Greg L
You can also do the following:
您还可以执行以下操作:
unless params.values_at(:one, :two, :three, :four).includes?(nil)
... excute code ..
end
I tend to use the above solution when I want to check to more then one or two params.
当我想检查超过一两个参数时,我倾向于使用上述解决方案。
.values_at returns and array with nil in the place of any undefined param key. i.e:
.values_at 返回并用 nil 代替任何未定义的参数键的数组。IE:
some_hash = {x:3, y:5}
some_hash.values_at(:x, :random, :y}
will return the following:
将返回以下内容:
[3,nil,5]
.includes?(nil) then checks the array for any nil values. It will return true is the array includes nil.
.includes?(nil) 然后检查数组是否有任何 nil 值。如果数组包含 nil,它将返回 true。
In some cases you may also want to check that params do not contain and empty string on false value.
在某些情况下,您可能还想检查 params 是否不包含假值上的空字符串。
You can handle those values by adding the following code above the unless statement.
您可以通过在除非语句上方添加以下代码来处理这些值。
params.delete_if{|key,value| value.blank?}
all together it would look like this:
总之它看起来像这样:
params.delete_if{|key,value| value.blank?}
unless params.values_at(:one, :two, :three, :four).includes?(nil)
... excute code ..
end
It is important to note that delete_if will modify your hash/params, so use with caution.
重要的是要注意 delete_if 会修改您的哈希/参数,因此请谨慎使用。
The above solution clearly takes a bit more work to set up but is worth it if you are checking more then just one or two params.
上面的解决方案显然需要更多的工作来设置,但如果您检查的参数不止一两个参数,则值得。
回答by Andres Ehrenpreis
In addition to previous answers: has_key?and has_value?have shorter alternativesin form of key?and value?. Ruby team also suggestsusing shorter alternatives, but for readabilitysome might still prefer longer versions of these methods.
除了以前的答案:has_key?与has_value?具有较短的替代品在形式key?和value?。Ruby 团队还建议使用较短的替代方案,但为了可读性,有些人可能仍然更喜欢这些方法的较长版本。
Therefore in your case it would be something like
因此,在您的情况下,它将类似于
if params.key?(:one) && params.key?(:two)
... do something ...
elsif params.key?(:one)
... do something ...
end
NB! .key?will just check if the key exists and ignores the whatever possible value. For ex:
注意!.key?只会检查键是否存在并忽略任何可能的值。例如:
2.3.3 :016 > a = {first: 1, second: nil, third: ''}
=> {:first=>1, :second=>nil, :third=>""}
2.3.3 :017 > puts "#{a.key?(:first)}, #{a.key?(:second)}, #{a.key?(:third), #{a.key?(:fourth)}}"
true, true, true, false

