jquery - Ruby on Rails redirect to view on successful AJAX load for slow controller action -
in ruby on rails, correct way render ajax 'spinner' while waiting slow controller action, , redirect correct view when complete?
considering attempt below:
controller
# app/assets/controllers/static_pages_controller.rb class staticpagescontroller < applicationcontroller def index end def slow_action sleep 3 # take nap respond_to |format| format.js {render js: "window.location = '#{slow_action_path}';"} format.html {} end end end
the index page jquery render spinner div:
<!-- app/views/static_pages/index.html.erb --> <h1>index page</h1> <li><%= link_to "a link slow action", slow_action_path, :remote => true %></li> <div id="spinner"> <p>loading... please patient.</p> </div> <script type="text/javascript"> $("#spinner").hide(); // hide spinner $(document).ajaxsend(function(r, s) { $("#spinner").show(); }); $(document).ajaxstop(function(r, s) { $("#spinner").fadeout("fast"); }); </script>
and, it's worth, slow_action's view:
<!-- app/views/static_pages/slow_action.html.erb --> <h1>this slow action</h1>
the problem (i think) window.location
call calling same action again, , server sleeping on again. correct way implement this?
we use spawn
aka spawnling
- https://github.com/tra/spawnling
you spawn off thread handle slow stuff , redirect different action/template periodically checks whether it's done yet. template displayed in modal-style box hovers on page. test whether app done yet creation of temp files, , tracking pid of spawned thread, use database instead if wanted.
your problem you're trying do thing , see if thing finished yet in same place. should have different, generic, action see if task finished yet, takes parameter allow test whether task finished.
Comments
Post a Comment