2012-08-16 44 views
1

我試圖在Rails 3.2應用程序中使用Google可視化庫。我在application.html.erb以下谷歌可視化庫不加載Rails 3.2和咖啡

<head> 
<title>Website</title> 
<%= stylesheet_link_tag "application", media: "all" %> 
<%= javascript_include_tag "application" %> 
<script src="http://www.google.com/jsapi" type="text/javascript"></script> 
<%= csrf_meta_tags %> 
</head> 

我已經在我的stuff.js.coffee如下:

google.load('visualization', '1', packages: ['gauge']) 
google.setOnLoadCallback(drawChart) 
drawChart -> 
    data = google.visualization.arrayToDataTable([ 
    ['Label', 'Value'], 
    ['Memory', 80], 
    ['CPU', 55], 
    ['Network', 68] 
    ]) 
    options = { 
    width: 400, height: 120, 
    redFrom: 90, redTo: 100, 
    yellowFrom:75, yellowTo: 90, 
    minorTicks: 5 
    } 
    chart = new google.visualization.Gauge(document.getElementById('result')) 
    chart.draw(data, options) 

我得到一個js錯誤時的頁面加載:未捕獲ReferenceError:谷歌沒有定義

如果我將圖表設置/創建js上移到application.hmtl.erb中,它一切正常。顯然沒有按照正確的順序在這裏加載。

回答

1

嘗試,包括谷歌的JSAPI文件之前的application.js

<head> 
<title>Website</title> 
<%= stylesheet_link_tag "application", media: "all" %> 
<script src="http://www.google.com/jsapi" type="text/javascript"></script> 
<%= javascript_include_tag "application" %> 
<%= csrf_meta_tags %> 
</head> 

我看到這個解決方案this github issue,它爲我工作。