I am using google map api , I have “business_locations” array
defined in view it contains latitude and longitude in following format
lng-142.122745lat42.674224
lng-112.122745lat46.674224
lng-122.122745lat40.674224
I am sending locations array to partial view following way
= render :partial => ‘coupons/search’, :locals => {:locations =>
business_locations}
to the javascript present on “_search.html.haml” here(in javascript) i
have function google_map() and addMarker() which adds no of markers
equal to length of array so i am doing
Here j variable is not set. How I set “j” variable???
function addMarker(latitude, longitude) {
var marker = new GMarker(new GLatLng(latitude, longitude));
}
where ("#{locations[0]}") is lng-142.122745lat42.674224
where ("#{locations[1]}") is lng-112.122745lat46.674224
where ("#{locations[2]}") is lng-112.122745lat46.674224
As a slightly more general solution, rather than wrapping the “” around
the erb block, if you call inspect on the object inside then it will
have quotes added if it’s a string but not if it’s a number, and if it’s
an array it will be shown in the right format, etc. So, .inspect makes
things print out in such a way that they ‘look right’ and can be used as
is.
(In the example above it looks like you need a number rather than a
string. )
#in rails controller @cars = Car.find(:all)
#in your js section in a view page
var carNames = <%= @cars.collect(&:name).inspect %>;
=> var carNames = [“Micra”, “Primera”, “Almeira”];
var firstCarModel = <%= @cars.first.model.inspect %>;
=> var firstCarModel = “Nissan”;
an array it will be shown in the right format, etc. So, .inspect makes
things print out in such a way that they ‘look right’ and can be used as
is.
I would use to_json since that is explicitly making things that
javascript understands - it’s mostly a coincidenance that inspect
coincides with this for certain types objects (for example with a hash
you need to_json)