Updating server progress on a Rails application

I want to download and then process the file in a Ruby on Rails application. The file upload is usually quite short, but server-side processing may take some time (more than 20 seconds), so I want to give the user some kind of indicator - something is better than the meaningless "processing ..." screen.

I am trying to use the following code in a view

<%= periodically_call_remote(:url => {:action => 'progress_monitor', :controller => 'files'}, :frequency => '5', :update => "setProgress('progressBar','5')" ) %> 

Parameter contents: update is javascript that I want to run every 5 seconds.

and the following code is in the file controller

 def progress_monitor render :text => 'whatever' end 

In the end, the progress_monitor method will return the current progress as an integer (% complete) and will be passed to javascript 'setProgress' code (which will update the item on the screen)

However, I am struggling to get the correct response from the server, which can then be passed to javascript.

Can someone help, or am I approaching this wrong?

I have the following question: I originally updated this question, but the update was varied enough to guarantee a new question here .

+4
source share
2 answers

periodically_call_remote () updates the div. It will not call your javascript function. I am not a javascript guru, but to solve your problem you have to make your own xmlhttp call. If I were you, I would use a prototype ajax request

http://www.prototypejs.org/api/ajax/request

and use javascript settimeout or setinterval for polling periodically

http://www.elated.com/articles/javascript-timers-with-settimeout-and-setinterval/

hope this helps, because in fact I came across the same prb =)

+2
source

In the parameter :update , only the list that you want to :update should be specified, does NOT have Javascript that you want to evaluate.

Rails helpers are still very good for this situation, there is no need to write a lot of custom JS. If you want to run JS upon return, an easy way would be to create an RJS template. If you want to do this with pure HTML, just visualize the view (either partial or return directly from the render method with an HTML progress bar) and put the progress bar div in the option :update periodically_call_remote .

0
source

Source: https://habr.com/ru/post/1277062/


All Articles