I have this array:
--- !map:ActiveSupport::HashWithIndifferentAccess
search: !map:ActiveSupport::HashWithIndifferentAccess
address: test
I apply the following to it:
address = params['search']['address']
if address
# do something
end
If there is no search object and no address object, this code raises an error:
You have a nil object when you didn’t
expect it! You might have expected an
instance of Array. The error occurred
while evaluating nil.[]
What’s the way in ruby to handle something like that?
Firstly…
Just to clarify that it is actually a specialised
Hash.Secondly…
You can do something like this: