简体   繁体   English

Ruby on Rails:在控制器中多次更新页面

[英]Ruby on Rails: Update page multiple times in controller

So I have a rather long controller function that performs various tasks, and it takes around 10-15 seconds. 因此,我有一个相当长的控制器功能,可以执行各种任务,大约需要10-15秒。 I want to be able to update the user on the status of the function as it goes along (progress bar). 我希望能够随着功能的进展(进度条)向用户更新功能的状态。 Ideally, the function is called as an Ajax request, and would then update the page as it goes along. 理想情况下,该函数被称为Ajax请求,然后会随着页面的进行更新。 This would work except you can only update the page once per action. 这将起作用,除了每个操作只能更新一次页面。 Any ideas? 有任何想法吗? Thank you. 谢谢。

This is what I want to do in code: 这是我要在代码中执行的操作:

def long_function
 updatePage "Starting long_function"
 #some code which take 5 seconds"
 updatePage "Finished with first part"
 #some more code which takes a while
 updatePage "Finished second part"
 #last code part
 updatePage "Finished!"
end

maybe something like this: trigger a background task and give it an id that you return. 也许是这样的:触发一个后台任务,并给它返回一个ID。 The background task could write messages to the database tagged with that id and you could have another action that lets you long-poll for the messages and thus update the user. 后台任务可以将消息写入带有该ID标记的数据库,并且您可以执行另一项操作,使您可以对消息进行长时间轮询 ,从而更新用户。 The last message will then be the answer. 最后一条消息将成为答案。

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

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