Flamine Flamine - 2 years ago 86
Ruby Question

Rails action is calling only once, but i call it every second in my ajax

Title is incorrect, but you might understand it.
I have this code in .coffee:

$.ajax(url: "/get_current_result_number").done (html) ->
$("#results").append html
setInterval( $.ajax, 1000 )

And in the controller:

def get_current_result_number
respond_to do |format|
format.html { render :xml => "<progress value=#{$status[:current]} max=#{$status[:all]} ></progress>" }

Global variable $status has data for progresbar, and i want to see it in real time, without refresh page. I see requests in rails console permanently. Action get_current_result_number is call only one, with first request, and dont call with next.
Where is error? (in code, not in English))

Answer Source

$.ajax is not your defined ajax call. You can rewrite it like this:

getCurrentResults = ()->
  $.ajax(url: "/get_current_result_number").done (html) ->
    $("#results").append html

setInterval(getCurrentResults, 1000)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download