Reputation: 14514
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.
Upvotes: 0
Views: 1625
Reputation: 304
Another meaningful and simplest way of doing it would be
@json_obj = []
@sog.each do |kon|
@json_obj << {"id":"kon.id",
"titel":"kon.titel.force_encoding('UTF-8')",
"url":"kon.photo.image.url"
}
You don't need to worry about the comma anymore. Hope this helps.
Upvotes: 0
Reputation: 160311
Several options, but one is use each_with_index
and add the comma iff it's not the last iteration.
You could collect
JSON strings and join
them with ","
, eliminating the need to check.
Or create a method that serializes the object to JSON, avoiding all the busywork in the view layer.
Upvotes: 3