[英]Passing values from a Ruby on Rails controller into Javascript to render
I've got this JS highchart that I'm trying to pass some values into that are calculated in my pages_controller
. 我有这个JS高图表,我正在尝试将一些值传递到在我的
pages_controller
中计算出的值。 I've tried this with global and instance variables and neither seem to work in rendering. 我已经使用全局变量和实例变量进行了尝试,但似乎都无法在渲染中使用。 I guess the value isn't passed in.
我猜该值未传递。
In my pages_controller
I have: 在我的
pages_controller
我有:
> @nil_ref = (@counts[nil]/total.to_f)*10
> @email_ref = (@counts["email"]/total.to_f)*10
> @cpc_ref = (@counts["cpc"]/total.to_f)*10
> @display_ref = (@counts["display"]/total.to_f)*10
And then in my /public/highcharts.js file I have: 然后在我的/public/highcharts.js文件中,我有:
var chart1; // globally available
$(document).ready(function () {
var chart = new Highcharts.Chart({
chart: {
renderTo: 'container',
type: 'pie'
},
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
yAxis: {
},
legend: {
layout: 'vertical',
floating: true,
backgroundColor: '#eeeeee',
align: 'right',
verticalAlign: 'top',
y: 60,
x: -60
},
tooltip: {
formatter: function() {
return '<b>'+ this.series.name +'</b><br/>'+
this.x +': '+ this.y;
}
},
plotOptions: {
},
series: [{
data: [<%= @nil_ref %>, <%= @email_ref %>, <%= @cpc_ref %>, <%= @display_ref %>] //These need to be global vars
}]
});
});
When I inspect the element in Chrome, I get Uncaught SyntaxError: Unexpected token
at the line with the Ruby tags in the JS. 当我检查Chrome中的元素时,在JS中的Ruby标签所在的行中出现
Uncaught SyntaxError: Unexpected token
。
Any ideas how to successfully pass these values in? 有什么想法可以成功地传递这些值吗?
如果RoR数据是字符串,则需要这样做:
data: ["<%= @nil_ref %>", "<%= @email_ref %>", ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.