Note that there are some explanatory texts on larger screens.

plurals
  1. POUse of a loop in javascript using an instance varibale as data source from rails controller
    text
    copied!<p>I need to display a google map with several markers. The problem I have is to set all the markers on the map. I have a city controller and a map view. Here is the relevant code:</p> <p>city_cntroller.rb</p> <pre><code>def map @city = City.find(params[:id]) @mapcenter = @city.geocode @businesses = @city.businesses @numberofbusinesses = @city.businesses.count end </code></pre> <p>map.html.haml</p> <pre><code>%script{:type =&gt; "text/javascript", :charset =&gt; "utf-8", :src =&gt; "http://maps.googleapis.com/maps/api/js?v=3.6&amp;sensor=false&amp;region=IN"} %script{:type =&gt; "text/javascript"} function initialize() { var cityLatlng = new google.maps.LatLng( = @mapcenter[0] , = @mapcenter[1] ); var options = { zoom: 12, center: cityLatlng, mapTypeId: google.maps.MapTypeId.ROADMAP }; var map = new google.maps.Map(document.getElementById("map_canvas"), options); var businesscount = ( = @numberofbusinesses ) for (var i = 0; i &lt;= businesscount; i++) { var marker = new google.maps.Marker({ position: new google.maps.LatLng( = @businesses[i].latitude , = @businesses[i].longitude ), map: map, }); }; } %body{ :onload =&gt; "initialize()" } %div#map_canvas{:style =&gt; "width: 900px; height: 350px;"} </code></pre> <p>The problem is that "i" is not accepted when I try to loop through @businesses, the error message is</p> <p>undefined local variable or method `i' for #&lt;#:0xbd2d970></p> <p>How can i loop through @businesses to place all the markers on the map?</p> <p>Thanks for your help!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload