In my JSON view I have:
[
<% @sog.each do |kon| %>
{"id":"<%= kon.id %>","titel":"<%= kon.titel.force_encoding("UTF-8") %>","url":"<%= kon.photo.image.url %>"},
<% end %>
]
How do I remove the last comma in the loop? The JSON is not working because there is a comma in the end.
Several options, but one is use
each_with_indexand add the comma iff it’s not the last iteration.You could
collectJSON strings andjointhem with",", eliminating the need to check.Or create a method that serializes the object to JSON, avoiding all the busywork in the view layer.