1

Right now I get:

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

I would like to return "" instead of null

But as default. How to achieve that. I don't want to add m.dosage || "" for each attribute in my application.

I am keeping my jbuilder views in views/api/documents/_document.json.jbuilder

json.id document.id
json.category document.category # sometimes this is nil
json.note document.note
json.attachments document.attachments do |attachment|
  json.url URI.join(request.url, attachment.url).to_s
end
json.created_at document.created_at
json.updated_at document.updated_at
tomekfranek
  • 6,852
  • 8
  • 45
  • 80

1 Answers1

0

Is this what you are looking for?

def replace_null(yourhash)
  yourhash.each do |k,v|
    if v == nil then
      yourhash[k] = ""
    end
  end
end

or

yourhash.each{|k,v| a[k] = "" if v == nil}
Pol0nium
  • 1,346
  • 4
  • 15
  • 31
  • Hmm but I am keeping all my jbuilder views in api/views/document.json.jbuilder file. – tomekfranek Oct 22 '13 at 10:59
  • You can put ruby code in your view. Have a look here : http://stackoverflow.com/questions/14459750/render-a-jbuilder-view-in-html-view – Pol0nium Oct 22 '13 at 11:02