on Rails应用程序更新服务器端的进步服务器端、应用程序、on、Rails

2023-09-10 13:49:18 作者:白泽.

我要上传,然后处理在一个Ruby on Rails应用程序的文件。文件上载通常很短,但服务器端处理可能需要一些时间(超过20秒),所以我想给用户的一些指标 - 的东西比一个毫无意义的处理...屏幕更好。

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

我尝试使用下面的code视图

I'm trying to use the following code in the view

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

中的内容:更新参数是我希望运行每5秒的JavaScript

The content of the :update parameter is the javascript I want to run every 5 seconds

和以下code是在文件控制器

and the following code is in the files controller

def progress_monitor
  render :text => 'whatever'
end

最终,progress_monitor方法将返回当前进度的整数(%完成)以及将要传递到setProgress'javascript的code(将更新的屏幕元素)

Eventually the progress_monitor method will return the current progress as an integer (% complete) and that will be passed into the 'setProgress' javascript code (that will update an on screen element)

不过,我努力让来自可随后被传递到JavaScript的服务器正确的响应。

However, I'm struggling to get a correct response from from the server that can then be passed into javascript.

谁能帮助,还是我处理这个错误的方式?

Can anyone help, or am I approaching this the wrong way?

有一个后续的问题这一点,我原本更新了这个问题,但更新是十分不同,值得一个新的问题,here.

There is a follow up question to this, I originally updated this question but the update was sufficiently different to warrant a new question, here.

推荐答案

periodically_call_remote()更新一个div。它不会叫你的JavaScript功能。我不是JavaScript的大师,但解决你的问题,你应该做你自己的XMLHTTP电话。如果我是你,我会使用原型的Ajax请求

periodically_call_remote() updates a div. It won't call your javascript function. I'm no javascript guru, but to solve your problem, you should do your own xmlhttp call. If I were you, I'd use prototype's ajax request

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

和使用JavaScript的setTimeout的或setInterval的做定期轮询

and use javascript's settimeout or setinterval to do the periodic polling

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

希望这有助于COS其实我已经遇到了同样的PRB太=)

hope this helps cos actually I've encountered the same prb too =)