开发者

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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜