regedarek regedarek - 2 years ago 143
Ruby Question

How to returns empty string instead of null in jbuilder views?

I have simple jbuilder view

pharmaceutic.dosage => nil

My rendered json looks like below:

{"id":1,"name":"HerzASS ratiopharm","dosage":null}

I would like set up for all jBuilder views that when some attribute is
it should be rendered as empty string.

{"id":1,"name":"HerzASS ratiopharm","dosage":""}

How to achieve that?

Answer Source

nil.to_s #=> "" so, you can simply add .to_s
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download