简体   繁体   中英

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. That means the view should be updated every 5 minutes to show all the data got so far . 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. 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. 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). 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

and another one would be using Ajax updated (you can do this using rails defaults ajax library)

code example would be,

'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

  • note - you much include the rails default javascript library

hope this helps

cheers, sameera

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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