When I try this, I get {a: 1, b:} for c not present. When c is present I get {a:1,b:1} which is right. But how can I hide the node b conditionally so that I could only get {a:1}?
{"a": <%= json @teams.count %>
,"b": <%= json @teams.num if @c.present? %>}
You can pass a ruby hash to
jsonand it will figure it out correctlyThe problem with your method is that you are trying to render the string yourself, but
nilis being rendered as blank space instead of an empty string''. You could theoretically fix it like this:but you can avoid most of the hassle if you use the first method.