简体   繁体   English

在Rails应用程序上更新服务器端进度

[英]Updating server-side progress on Rails application

I want to upload and then process a file in a Ruby on Rails app. 我想上传然后在Ruby on Rails应用程序中处理文件。 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. 文件上传通常很短,但是服务器端处理可能需要一些时间(超过20秒),因此我想给用户一些指示-比无意义的“处理中...”屏幕更好。

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')"
                 ) %>

The content of the :update parameter is the javascript I want to run every 5 seconds :update参数的内容是我想每5秒运行一次的javascript

and the following code is in the files controller 并且以下代码在文件控制器中

def progress_monitor
  render :text => 'whatever'
end

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) 最终,progress_monitor方法将以整数(已完成百分比)的形式返回当前进度,并将其传递到“ setProgress” JavaScript代码中(这将更新屏幕上的元素)

However, I'm struggling to get a correct response from from the server that can then be passed into JavaScript. 但是,我正在努力从服务器获取正确的响应,然后将其传递到JavaScript中。

Can anyone help, or am I approaching this the wrong way? 任何人都可以提供帮助,还是我以错误的方式处理此问题?

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() updates a div . periodically_call_remote()更新div It won't call your JavaScript function. 它不会调用您的JavaScript函数。 I'm no JavaScript guru, but to solve your problem, you should do your own xmlhttp call. 我不是JavaScript专家,但是要解决您的问题,您应该执行自己的xmlhttp调用。 If I were you, I'd use prototype's AJAX request 如果我是你,我会使用原型的AJAX请求

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

and use JavaScript's settimeout or setinterval to do the periodic polling 并使用JavaScript的settimeout或setinterval进行定期轮询

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

hope this helps cos actually I've encountered the same prb too =) 希望这有助于cos,实际上我也遇到过相同的PRB =)

The :update option should only list the div you want to update, NOT have Javascript that you want to evaluate. :update选项应该只列出您要更新的div,而没有要评估的Javascript。

The Rails helpers are still very good for this situation, there's no need to write much custom JS. Rails的助手在这种情况下仍然非常有用,无需编写很多自定义JS。 If you wish to execute JS on return, an easy way would be to render an RJS template. 如果您希望在返回时执行JS,一种简单的方法是呈现RJS模板。 If you wish to do it with pure HTML, simply render a view (or partial, or return directly from the render method with the HTML of the progress bar) and put the div of the progress bar in the :update option of the periodically_call_remote method. 如果你想用纯HTML做到这一点,只是呈现一个视图(或部分,或者直接从返回render方法与进度条的HTML),并把进度条的DIV中:update了的选项periodically_call_remote方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM