Why is the second if statement failing in the following code block? The output from the console indicates that the private parameter is 0, so it should be passing? The private parameter is from a checkbox in the new form, that I’m using to set a boolean field in the model.
if (((params[:note])[:text]) != "")
logger.debug("passed first test")
logger.debug(((params[:note])[:private]))
if (((params[:note])[:private]) == 0)
logger.debug("passed second test")
end
end
console output
passed first test
0
Completed in 61ms (DB: 1) | 302 Found [http://localhost/notes]
Thanks for reading.
Form fields are submitted as strings so
params[:notes][:private]will actually contain"0"not0.You could use either
params[:notes][:private] == "0"orparams[:notes][:private].to_i == 0to get the comparison you’re after.If you want to treat non-integer values (including empty strings and missing values) differently from 0, you should not use
String#to_i. I would recommend checking the String value in these cases.You can use
Object#inspect(as mentioned by @sepp2k) andObject#classwhen debugging to get a better idea of what types things are. When usingscript/console, I findap(orpp) to be rather handy.A side note: In Ruby you don’t need so many parentheses. Here’s the your example after a little bit of cleanup: