In my Rails 3.2.3 application I have the following code in the application.html.erb to refresh the whole page every 30 seconds:
<meta http-equiv="refresh" content="30">
It works well (it’s an internal intranet and we need to make sure everyone is looking at the latest information regardless of how long they have left the page open on their computer) and anything more complex, such as AJAX, feel unnecessary.
What I would like to do is have some kind of ‘unless’ statement wrapping the meta tag such as:
<% unless page.contains('input') %>
<meta http-equiv="refresh" content="30">
<% end %>
This would prevent the page refreshing after 30 seconds if the page contains a form input field. As it stands at the moment the page will refresh even if you’re halfway through filling in a page.
Is anything like this possible. I realise the above code example would never work, it’s purely to explain what I’m trying to achieve.
One possibility is to check what the current page’s action is as stated in the documentation:
<% current_page?(:action => 'new') %>
but I’m not entirely sure if that would work.
I would probably handle this with a script instead, and remove the
<meta http-equiv='refresh'>tag.If you’re using jQuery or another library, append this to the DOM ready functions as in: