0

i have a view in my postgres database that returns an array on my frequencies column. unfortunately, it sometimes returns values like {NULL} (due to the raw data).

in my rails view, i have something like:

dataset = [
    <% @item.each do |i| %>
        {   
            name: "<%= i.device %>" 
            lng: <%= i.longitude %> 
            lat: <%= i.latitude %>
            frequencies: <%= i.frequencies.to_s.html_safe %>
        },
    <% end %>
]

which appears to work great - except when it reaches a record that contains {NULL}:

in the javascript console it shows:

Uncaught ReferenceError: nil is not defined 

and in the html it shows:

  ...
  }, {
    name: "blah",
    lng: -122.2,
    lat: 37.4,
    frequencies: [nil]
  }, { 
  ...

i could fix this by iterating through the list in the controller, but i think this would be rather long winded (and a waste of cycles).

is there a way i can get the erb to output the 'correct' [] in (instead of [nil]) json when it's null?

1 Answer 1

3

Try this:

 <%= i.frequencies.compact %>

compact example:

 [nil].compact #=> []
 [1,2, nil, 3, nil].compact  #=> [1,2,3]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.