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:
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.
In some cases you may also want to check that params do not contain and empty string on false value.
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.
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.
params
is a Rails controller method (that happens to return a HashWithIndifferentAccess), it is about Rails. – mu is too short