I show flash messages in my rails app using javascript (essentially every flash message is an overlay div).
The following partial is included in my main layout file and it takes care of displaying the flash. I notice that if I refresh a page, the same flash message keeps reappearing. Anyway to get rid of this? I know I can call a flash.discard though not sure what's the best place to do that and I don't want another controller call just to discard the flash.
<% if !flash.nil? %>
<% if flash[:notice] %>
<script type="text/javascript">
//<![CDATA[
showFlash("<%= flash[:notice] %>", "flash-notice");
//]]>
</script>
<% end %>
<% if flash[:success] %>
<script type="text/javascript">
//<![CDATA[
showFlash("<%= flash[:success] %>", "flash-success");
//]]>
</script>
<% end %>
<% if flash[:error] %>
<script type="text/javascript">
//<![CDATA[
showFlash("<%= flash[:error] %>", "flash-error");
//]]>
</script>
<% end %>
<% end %>
function showFlash(msg, tp) {
if ($('#flash_messages_container').length > 0) {
flashAnimatedHide();
}
var flashContainer = $("<div id='flash_messages_container' class='flash-messages " + tp + " '></div>");
flashContainer.prepend("<div id='flash_messages_content'>" + msg + "<span class='go-right sprite close' id='close_flash'></span></div>");
$('body').prepend(flashContainer);
flashContainer.animate({
opacity : .1
}, '500', function(){
flashContainer.css('opacity', 1)
})
$('body').css('margin-top', flashContainer.outerHeight());
$('#close_flash').click(function() {
flashAnimatedHide();
$('body').css('margin-top', '0');
});
}
UPDATE
I suspect this is happening since I starting using etags for cient side and reverse proxy caching. So I guess the refresh is simply loading the cached page, which contains the flash messages in the flash container. So how do people discard flash when caching is enabled?