Topic: accessing rails class variables in javascript

Hi-
How can I access  the class variables (@something) within onLoad javascript
functions?

I pass an array of customers object to a .rhtml file. When it is
rendered on the browser, I call an onLoad function. I need to access the
@customers objects array in my onLoad function. How can I do that. For
example, I need to get to @customers[0][:customer_name] - how to do
that? thanks

Re: accessing rails class variables in javascript

Javascript does not have access to Ruby variables. For one thing the javascript is client side, and the Ruby variables are server side. The closest thing you have to this is AJAX with RJS, but that may not be what you need if you want to access the array in the onLoad call.

The solution is probably specific to what you are trying to do, but one possible answer is to loop through the customers in Ruby and re-create the array in Javascript. Sorry I can't provide example code as it's too specific to your problem.

Railscasts - Free Ruby on Rails Screencasts