'Ruby Each loop in Slim

I want to be able to loop through an instance var through a javascript but I'm not quite sure how to make it work.

javascript:
  [email protected] do |spot|
      map.addMarker({
        lat: "#{spot.latitude}",
        lng: "#{spot.longitude}",
        title: "spot",
      });
  });

What I tried: Using <% @spots.each do |spot| %> and without the quotes for the #{}



Solution 1:[1]

easy fix but not efficient if spots are too long.

[email protected] do |spot|
  javascript:
    map.addMarker({
      lat: "#{spot.latitude}",
      lng: "#{spot.longitude}",
      title: "spot",
    });

2. loop in js instead of view context

javascript:
  var spots = #{@spots.to_json};
  $(spots).each(function(index, obj) {
    map.addMarker({
      lat: obj.latitude,
      lng: obj.longitude,
      title: "spot",
    });
  });

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1