我正在使用 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/