I tried code, that plused a lot of people - How to test if parameters exist in rails, but it didn't work():
我尝试了很多人的代码 - 如何测试rails中是否存在参数,但它不起作用():
if ( params.has_key?([:start_date]) && params.has_key?([:end_date]) )
I think, that is because of complicated params and if I write this:
我想,那是因为复杂的参数,如果我写这个:
if ( params.has_key?([:report][:start_date]) && params.has_key?([:report][:end_date]) )
gives me error
给我错误
can't convert Symbol into Integer
this doesn't work too:
这也行不通:
if ( params[:report][:start_date] && params[:report][:end_date] )
gives me error:
给我错误:
undefined method `[]' for nil:NilClass
It always go into else statement.
它总是进入其他声明。
Here are my params:
这是我的参数:
report:
start_date: 01/08/2012
end_date: 10/08/2012
Can someone help me ?
有人能帮我吗 ?
5 个解决方案
#1
12
if params[:report] && params[:report][:start_date] && params[:report][:end_date]
#2
#3
6
It would seem you need the following
看起来你需要以下内容
params[:report].present?
or
要么
params[:report].nil?
or
要么
params[:report].empty?
depending on what you are trying to check for
取决于您要检查的内容
#4
6
I have been looking for a better solution too. I found this one:
我一直在寻找更好的解决方案。我找到了这个:
Is there a clean way to avoid calling a method on nil in a nested params hash?
是否有一种干净的方法可以避免在嵌套的params哈希中调用nil上的方法?
params[:some].try(:[], :field)
You get the value or nil
if it does not exist.
如果不存在,则获得值或nil。
So I figured let's use try
a different way:
所以我认为让我们尝试一种不同的方式:
params[:some].try(:has_key?, :field)
It's not bad. You get nil
vs. false
if it's not set. You also get true
if the param is set to nil
.
不算太差。如果没有设置,你会得到零而不是假。如果param设置为nil,也会得到true。
#5
1
This works to:
这适用于:
if params[:report].has_key?(:start_date)
And maybe add this so you also check if it is not empty:
也许添加这个,所以你也检查它是否为空:
&& !params[:report][:start_date].empty?
#1
12
if params[:report] && params[:report][:start_date] && params[:report][:end_date]
#2
9
Cross-post answer from here:
来自这里的交叉答案:
Ruby 2.3.0 makes this very easy to do with #dig.
Ruby 2.3.0使用#dig非常容易。
h = { foo: {bar: {baz: 1}}}
h.dig(:foo, :bar, :baz) #=> 1
h.dig(:foo, :zot, :baz) #=> nil
#3
6
It would seem you need the following
看起来你需要以下内容
params[:report].present?
or
要么
params[:report].nil?
or
要么
params[:report].empty?
depending on what you are trying to check for
取决于您要检查的内容
#4
6
I have been looking for a better solution too. I found this one:
我一直在寻找更好的解决方案。我找到了这个:
Is there a clean way to avoid calling a method on nil in a nested params hash?
是否有一种干净的方法可以避免在嵌套的params哈希中调用nil上的方法?
params[:some].try(:[], :field)
You get the value or nil
if it does not exist.
如果不存在,则获得值或nil。
So I figured let's use try
a different way:
所以我认为让我们尝试一种不同的方式:
params[:some].try(:has_key?, :field)
It's not bad. You get nil
vs. false
if it's not set. You also get true
if the param is set to nil
.
不算太差。如果没有设置,你会得到零而不是假。如果param设置为nil,也会得到true。
#5
1
This works to:
这适用于:
if params[:report].has_key?(:start_date)
And maybe add this so you also check if it is not empty:
也许添加这个,所以你也检查它是否为空:
&& !params[:report][:start_date].empty?