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

前端 未结 5 883
悲哀的现实
悲哀的现实 2021-02-18 18:12

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

5条回答
  •  长情又很酷
    2021-02-18 19:13

    Use Ruby's 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.

提交回复
热议问题