Flash Notice Appearing Twice – Ruby on Rails

Ran into another flash notice issue, my messages were not disappearing after the first view. My code was as follows:

#Create flash warning
flash[:warning] = ("This product has been replaced: <a href='#{url_for(new_product)}'>" +new_product.title + "</a>").html_safe

It turns out that all you have to do to fix this issue is the following:

flash.now[:warning] = ("This product has been replaced: <a href='#{url_for(new_product)}'>" +new_product.title + "</a>").html_safe

This blog gives a brief overview of when to use flash.now[notice] and when to use flash[:notice]. A basic rule of thumb is that if you’re using a redirect, use flash[:notice], if you’re simply rendering a view, use flash.now[:notice].

Leave a Reply