I have a piece of code that enables or disables selection lists when the appropriate checkbox is clicked, however if they are all disabled on post it will not send these in a the params list. So I need to execute code only if they exist.
I’ve used:
if params[:id][:id2].nil?
and
if params[:id][:id2].blank?
also
if params[:id][:id2].empty?
not to mention simply:
if params[:id][:id2]
And all give the same error…”You have a nil object where you weren’t expecting it.” How can I fix this error?
Probably
params[:id]is nil, check forparams[:id].nil?.EDIT: The comments below state correctly that
blank?is better if you seek to evaluate against eithernil?orempty?.