jqueryruby-on-railsrubyjsapi

Getting "(anonymous function)" error in the terminal


I am trying to implement speedometer in my rails application using this link

But I am getting following error

Uncaught ReferenceError: google is not defined
(anonymous function)

Here is my sample coffee script code

$(document).ready ->
  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('widget-inner'))
      chart.draw data, options
      setInterval (->
        data.setValue 0, 1, 40 + Math.round(60 * Math.random())
        chart.draw data, options
        return
      ), 13000
      setInterval (->
        data.setValue 1, 1, 40 + Math.round(60 * Math.random())
        chart.draw data, options
        return
      ), 5000
      setInterval (->
        data.setValue 2, 1, 60 + Math.round(20 * Math.random())
        chart.draw data, options
        return
      ), 26000
      return
    
    google.load 'visualization', '1.0', 'packages': [ 'gauge' ]
    google.setOnLoadCallback drawChart
    

and here is my index.html.haml file in which I am trying to add the given speedometer.

.content-container.widgets-container
  %h2 "This is Client Manager Dashboad page"
  = link_to "Add New Widget", admin_widgets_path, :class => "btn btn-info"
  .widgets-wrapper
    #widget-1.col-sm-4
      .widget-inner#widget-inner

Also I have added jsapi script file in the layout

<script type="text/javascript" src="https://www.google.com/jsapi"></script>

Can any one tell me how to resolve the give error.

I am using rails 3.2.11 and ruby 1.9.3


Solution

  • It looks like the jsapi script hasn't finished loading when the $(document).ready fires up. Ready doesn't mean all loaded. Place the jsapi script inclusion before the $(document).ready script.