简体   繁体   English

如何在Rails中每五分钟更新一次视图

[英]how update a view every five minutes in rails

I have a button StartMonitor ,and after user pressing this button, I will start checking current available memory every 5 minutes. 我有一个StartMonitor按钮,在用户按下此按钮后,我将每5分钟开始检查一次当前的可用内存。 That means the view should be updated every 5 minutes to show all the data got so far . 这意味着该视图应每5分钟更新一次,以显示到目前为止的所有数据 I can get the data , but I have no idea how to update the view. 我可以获取数据,但是我不知道如何更新视图。 What would you recommend?Thanks. 您会推荐什么?谢谢。

You will have to actively poll this from the client. 您将必须主动从客户端轮询。 After the view has rendered, the connection to the client is lost. 渲染视图后,与客户端的连接将丢失。 You can not, from the server side, push information to the browser. 您不能从服务器端将信息推送到浏览器。

Use JavaScript or a Meta-Tag with "reload" to reload the view every 5 minutes. 每隔5分钟使用JavaScript或带有“重新加载”的元标记重新加载视图。 For example 例如

<meta http-equiv="refresh" content="300;url=/controller/action/id">

If you reload the entire page (either via JS or the Meta-Tag) and the reload does not succeed (eg Server Busy Error) your reload code is no longer executed, because the browser now shows an error page not containing your reload-code. 如果您重新加载整个页面(通过JS或Meta-Tag)并且重新加载失败(例如服务器忙错误),则您的重新加载代码将不再执行,因为浏览器现在显示的错误页面不包含您的重新加载代码。 For example 例如

I suggest, update a div using AJAX and handle the error event yourself (ignore it, retry, inform the user. whatever fits your needs). 我建议使用AJAX更新div并自行处理错误事件(忽略,重试,通知用户。任何适合您的需求)。 That should be a rubust solution. 那应该是一个垃圾解决方案。

periodically_call_remote(:url => 'update', :frequency => '300', :update => { :success => "ok", :failure => "error" }

You actually have several ways of doing this 您实际上有几种方法可以做到这一点

one is what 'phisch' is suggested 一个是建议的“ phisch”

and another one would be using Ajax updated (you can do this using rails defaults ajax library) 另一个将使用更新的Ajax(您可以使用rails默认的ajax库进行此操作)

code example would be, 代码示例是

'message_div', :url => { :action => 'message' }, :frequency => '20'%> 'message_div',:url => {:action =>'message'},:frequency =>'20'%>

where 'message_div' is the updating div action -> is the controller action and frequency is the updating frequency in seconds 其中'message_div'是更新div操作->是控制器操作,而频率是更新频率(以秒为单位)

  • note - you much include the rails default javascript library 注意-您包括了Rails默认的JavaScript库

hope this helps 希望这可以帮助

cheers, sameera 欢呼声,sameera

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

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