0

-Update-

found answer here: json, rails, parse error in javascript

also, see iltempo's answer below (thanks iltempo)

JSON / GOOGLE MAPS

PlacesController, index respond_to block

    format.json { render json: @places }    

places.js.erb (/assets)

    var markersArray = <%= @places.to_json %>;
    addMarkerArray(markersArray);

    function addMarkerArray(markersArray) {
        for(var i = 0; i < markersArray.length; i++) {
            var lt = markersArray[i].latitude;
            var lg = markersArray[i].longitude;
            ...
            ...
        }
    )

Firebug Error

    TypeError: markersArray is null 
    " for(var i = 0; i < markersArray.length; i++) { "  
3
  • can you post your full PlacesController? Commented Nov 9, 2012 at 7:57
  • Brad, thanks for trying to help but finally found stack question that answered it... stackoverflow.com/questions/4485856/… thanks again Commented Nov 9, 2012 at 8:16
  • Right on - I had overlooked the /assets. Commented Nov 9, 2012 at 8:20

1 Answer 1

0

You are not able to access the ERB template variable @places in a Javascript file placed in assets/. Instead call the function addMarkerArray() with the return value of the AJAX call you do to get the places data.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the response. Finally found a similar stack question here stackoverflow.com/questions/4485856/…

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.