how to pass ruby objects to javascript ?

hi all,

   I have following problem,

   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

function google_map(){
for(var j=0; j < "#{locations.length}"; j++)
  {
marker=addMarker(parseFloat("#locations[j][:lat]"),
parseFloat("#locations[j][:lng]"));
  }}

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

Wrap variables around <%= %> inside double quote
e.g.
for(var j=0; j < "<%=locations.length%>"; j++)

vlain wrote:

Wrap variables around <%= %> inside double quote
e.g.
for(var j=0; j < "<%=locations.length%>"; j++)

On Jul 22, 4:17�pm, Nilesh Kulkarni <rails-mailing-l...@andreas-s.net>

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";

for(var j=0; j < <%= @cars.length.inspect %>; j++)
=> for(var j=0; j < 3; j++)

Are you using the GeoKit Rails plugin?

vlain wrote:
> Wrap variables around <%= %> inside double quote
> e.g.
> for(var j=0; j < "<%=locations.length%>"; j++)

> On Jul 22, 4:17 pm, Nilesh Kulkarni <rails-mailing-l...@andreas-s.net>

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.

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)

Fred

thanks for reply

I have done it something like

= render :partial => 'coupons/search', :locals => {:locations =>
business_locations.}

in "_search.html.haml"
:javascript
  var locs = (#{@locations.to_json});

function google_map(){
for(var j=0; j < locs.length; j++)
  {
marker=addMarker(locs[i]['lat'], locs[i]['lng'])
  }}

and this is working fine !