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
collect
Method.A cleaner version of your solution, Xaxum, would be something like this:
def chart_values
@mpg_values.collect do |mpg_value|
[ mpg_value.date.to_s, mpg_value.mpg.round(2).to_f ]
end
end
The collect
method automatically generates an array of whatever the block results in, so you'll end up with something like this:
[
[2011-05-20, 18.56],
[2011-06-01, 18.47]
]
I also changed the mpg
value to use to_f
, which provide a number instead of a string.