flash[:notice] is shown on browser back again
Hej, i have a problem: I'm using flash[:notice] in my rails app. A User comes to the page and creates a new object. The flash-message says "Created." Now he clicks on a link on the page. If he is coming back by using the "back" button of his browser the flash massage i开发者_运维知识库s presented again. flash.now[:notice] doesnt work either (i think and i tested). The problem is that there is no new rendering of the file.
Do you have any idea to prevent that? Thanks!
The problem is not the flash variable, but the browser caching. This solution worked well for me using Rails 3.2 (from this thread How to prevent browser page caching in Rails):
in application_controller.rb:
before_filter :set_cache_buster
def set_cache_buster
response.headers["Cache-Control"] = "no-cache, no-store, max-age=0, must-revalidate"
response.headers["Pragma"] = "no-cache"
response.headers["Expires"] = "Fri, 01 Jan 1990 00:00:00 GMT"
end
I generally used following
<% if flash[:notice] %>
<%= flash[:notice] %>
<% flash[:notice]=nil %>
<% end %>
you can also use discard method
<% if flash[:notice] %>
<%= flash[:notice] %>
<% flash.discard(:notice) %>
Setting the cache to no-cache, no-store will result in unnecessary requests to your server. Instead you can use browser's localStorage / sessionStorage to achieve the desired effect.
I have written a detailed answer here: https://stackoverflow.com/a/41921476/6590834
I know the discard method, the problem is not the functionality of flash[:notice], the problem is that the browser isn't rendering the file again... I Think i have to fix it by using javascript...
精彩评论