javascript - 在 Ruby on Rails 的 haml View 中将 ruby​​ 数组传递给 javascript 代码 (jquery)

标签 javascript jquery ruby-on-rails ruby

我正在使用 jQuery/javascript 在 haml 模板上显示 Highcharts 图表。以下片段显示了 HighCharts 站点中演示代码的独立图表:

:javascript

  $(document).ready(function() {
    $("#tabs").tabs();
    new Highcharts.Chart({
      chart: {
        renderTo: 'volume_chart'
      },
      title: {
        text: 'Logarithmic axis demo'
      },
      xAxis: {
        tickInterval: 1
      },
      yAxis: {
        type: 'logarithmic',
        minorTickInterval: 0.1
      },
      tooltip: {
        headerFormat: '<b>{series.name}</b><br />',
        pointFormat: 'x = {point.x}, y = {point.y}'
      },
      series: [{            
        data: [1, 2, 4, 8, 16, 32, 64, 128, 256, 512],
        pointStart: 1
      }]
    });
  });

这很好用。现在,我正尝试在 show.html.haml 文件中设置来自 ruby​​/rails 数组的系列数据,如下所示:

...
- data_array        = [1, 2, 4, 8, 16, 32, 64, 128, 256, 512]
...
:javascript

  $(document).ready(function() {
  ...
      series: [{            
        data: "#{data_array}",
        pointStart: 1
      }]
    });
  }); 

这给了我以下错误:

undefined method `to_js' for [1, 2, 4, 8, 16, 32, 64, 128, 256, 512]:Array

如何将 data_array 从我的 ruby​​/rails 代码传递到 javascript/jquery 代码?

感谢任何帮助。

谢谢。

印度

最佳答案

require 'json'

...

"#{data_array.to_json}"

关于javascript - 在 Ruby on Rails 的 haml View 中将 ruby​​ 数组传递给 javascript 代码 (jquery),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10158109/

相关文章:

JQuery ui 可排序拖动问题

javascript - RightJs (right-rails) 是否已准备好用于 Rails3

ruby-on-rails - ruby rails : Interpreting a form input as an integer

ruby-on-rails - 使用辅助方法的 Rails 自定义验证消息

javascript - MongoDB 按 _id 的时间戳分组

javascript - 需要将 Chrome 开发工具中的性能监视器详细信息捕获到 Java 程序中

javascript - typeof jQuery.data() 有问题吗?

javascript - 检索当前用户的策略

javascript - React 组件已被 CORS 策略阻止 : No 'Access-Control-Allow-Origin' header is present on the requested resource

javascript - moment.js isValid 函数无法正常工作