I have an ajax request in my rails app that returns a variable from my controller action. Inside the controller action I have a loop that can take some time to get 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 render the results at each stage of the loop to the js.erb partial instead of waiting for the loop to finish. Is this possible without breaking the loop and ending the action prematurely before the loop finishes? Maybe something like this (pseudo code that is wrong):
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);