Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3 passing a rails array to javascript function which uses a javascript array

Tags:

I am trying to pass a rails array to a javascript function. The function will then use the array values in javascript so I need to keep it an array.

Here is how I am passing my array:

"graphit([<%=@mpg_values.join(",") %>])"

This produces this in the HTML

"graphit([#&lt;Mile:0x6ff0cf0&gt;,#&lt;Mile:0x6ff0c18&gt;,#&lt;Mile:0x6ff0b58&gt;])"

Which are not the values, they should be decimal numbers. I assume this is because it is a reference to the array so that I why I am getting the messed up values. If I do something like to_s I see the values but it has other stuff like the table and filed name in there as well.

Anybody know how I can get this rails array to a javascript function as an array?

Thanks in advance for any help.

like image 811
Xaxum Avatar asked Aug 25 '11 19:08

Xaxum


1 Answers

Since this still seems like an issue and nobody resolved it. Here goes

var js_array = [<%= raw @object.to_json %>];
like image 104
bokor Avatar answered Oct 19 '22 18:10

bokor