Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have simple jbuilder view

json.id pharmaceutic.id
json.name pharmaceutic.name
json.dosage pharmaceutic.dosage.name

When 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 nil it should be rendered as empty string.

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

How to achieve that?

like image 557
tomekfranek Avatar asked Aug 01 '13 13:08

tomekfranek


1 Answers

nil.to_s #=> "" so, you can simply add .to_s

json.id pharmaceutic.id
json.name pharmaceutic.name.to_s
json.dosage pharmaceutic.dosage.name.to_s
like image 53
user2641326 Avatar answered Nov 12 '22 01:11

user2641326