B Seven
B Seven

Reputation: 45943

How to update page with AJAX from Rails?

I have an action that takes a while to complete. At the start, it should let the user know "please wait..." Then, when finished, it should tell them "finished."

Currently the way I have it set up throws a double render error.

controller:

def do_work
  render 'do_work.js'
  .
  .  # takes a while...
  .
  .
  redirect_to :action => :work_is_done
end

do_work.js.erb:

$("#status").text("please wait...");

What is the best way to do this? Working in Rails 3 and jQuery.

Thanks.

Upvotes: 0

Views: 223

Answers (1)

bassneck
bassneck

Reputation: 4043

You have to redirect from do_work.js

$("#status").text("please wait...");
window.setInterval(function(){ 
  window.location.replace(<%= escape_javascript work_is_done_url %>) 
}, 6000);

6000 - is time in miliseconds after which the function will be triggered

upd

ofc you will need a named route work_is_done for this to work.

Upvotes: 2

Related Questions