javascriptjqueryruby-on-railsajaxruby-ffi

How do I get AJAX working using rails and a javascript timer?


The rails application that I am programming takes in data from an external source from a shared object via ruby's ffi gem. I have got the data to come in and display on the screen once but when i try to update with AJAX I cant seem to manipulate the data properly. I just want a few divs which contain the data values to be updated with new data.

Pane_controller:

def getAllData
 init
 a = getCurrent
 #a << getRIM
 a << getWaterTemp
 a << getCharge
 return a
end

def index
 @data = getAllData
 respond_to do |format|
  format.html
  format.js
 end
end

Coffeescript AJAX:

systemTick = ->
 $.ajax({
  url: "/"
 });
...
systemTickTimeout = setTimeout (->
 systemTick()), 1000

index.js.erb (i currently have this place in app/views/pane/ ):

$("#amps").html(<%= @data[0]%>);
$("#volts").html(<%= @data[1]%>);
$("#watts").html(<%= @data[2]%>);
$("#current-fuel-cell-state").html(<%= @data[3]%>);
$("#temperature-reading").html(<%= @data[4]%>);
$("#battery-charge").html(<%= @data[5]%>);

here are the routes I have as well:

root :to => 'pane#index'
get '/settings' => 'pane#settings'
get '/history' => 'pane#history'
get '/dashboard' => 'pane#dashboard'

Whenver i try this i get the error that there is no method [] for Nil which i know implies that the data is not being sent but i dont understand why?

EDIT* The nil no method [] is no longer there but i am still not able to display new data via AJAX the values in my js.erb file are not properly getting filled in.

index.html.erb:

<tr>
  <td id="amps"><%= "%.1f" % @data[0]%></td>
</tr>
<tr>
  <td id="volts"><%= "%.2f" % @data[1]%></td>
</tr>
<tr>
  <td id="watts"><%= "%.1f" % @data[2]%></td>
</tr>
<span class="value" id="battery-charge"><%=@data[5].to_i%></span>
<span class="value" id="temperature-reading"><%=@data[4].to_i%></span>
<span id="current-fuel-cell-state" style="display:none"><%=@data[3].to_i%></span>

also this is what is generated in the log:

Started GET "/" for 127.0.0.1 at 2013-10-23 15:30:00 -0400
Processing by PaneController#index as */*
Rendered pane/index.html.erb within layouts/application (0.5ms)
Completed 200 OK in 294ms (Views: 9.9ms | ActiveRecord: 0.0ms)

Solution

  • You should place your javascript template in app/views/panes/

    app/views/panes/index.js.erb (notice the change in the argument to html)

    $("#amps").html("<%= @data[0]%>");
    $("#volts").html("<%= @data[1]%>");
    $("#watts").html("<%= @data[2]%>");
    $("#current-fuel-cell-state").html("<%= @data[3]%>");
    $("#temperature-reading").html("<%= @data[4]%>");
    $("#battery-charge").html("<%= @data[5]%>");
    

    PanesController

    def getAllData
     init
     a = getCurrent
     #b = getRIM
     a << getWaterTemp
     a << getCharge
     return a
    end
    
    def index
     @data = getAllData
     respond_to do |format|
      format.html
      format.js
     end
    end