English 中文(简体)
在Rubiras的“黑色”观点中,通过垃圾阵列以 j(jquery)
原标题:passing ruby arrays to javascript code (jquery) in a haml view in Ruby on Rails

我正在使用jQuery/javascript,展示关于“haml”模板的图象。 这里是一张显示高Charts矿场白法独立图表的刀子:

: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
      }]
    });
  });

罚款。 现在,我正试图将系列数据从碎片/铁路阵列上。 html。

...
- 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

How can I pass data_array from my ruby/rails code to the javascript/jquery code?

给予任何帮助。

感谢。

Bharat

最佳回答
require  json 

...

"#{data_array.to_json}"
问题回答

没有必要使用<代码>至_json。

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

完美的工作





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签