I have an ajax request in my rails application that returns a variable from my controller action. Inside the controller action, I have a loop that may take some time to go through.
Controller
def myAction $j = 1 until $j > list_size do array << { :foo => $j } $j +=1; end @myvariable = array.to_json end
myAction.js.erb
var myVariable = JSON.parse("<%= escape_javascript(raw @myvariable) %>"); for (var k = 0; k < myVariable.length; k++) { $("#myDiv").append(myVariable[k].foo); }
I want to be able to display the results at each step of the loop in the js.erb part, and not wait for the loop to complete. Is this possible without breaking the cycle and ending the action prematurely before the cycle ends? Maybe something like this (wrong pseudocode):
Controller
def myAction $j = 1 until $j > list_size do array << { :foo => $j } render array.to_json $j +=1; end end
myAction.js.erb
var myVariable = JSON.parse("<%= escape_javascript(raw @myvariable) %>"); $("#myDiv").append(myVariable.foo);
javascript ajax ruby-on-rails
diasks2
source share