I'm facing an issue in my controller/action where some values turn out to be nil:
def my_action
@var1 = get_boolean_value || nil
@var2 = get_int_value || nil
@var3 = get_string_value || nil
# there are many more values, any of them might be nil
end
When I convert these values into a JSON object in a view, the nil values end up being rendered as empty spaces:
:javascript
window.MyObj = {
var1: #{@var1},
var2: #{@var1},
var2: #{@var3}
}
The problem is that when a value is nil, it is displayed as an empty space instead of nil itself.
:javascript
window.MyObj = {
var1: ,
var2: 33,
var2:
}
Any suggestions on how to fix this issue?