Posted about 11 years ago. Visible to the public. Linked content.

Why a Rails flash message is shown twice

You set a flash message and it shows up as it should. However, it is displayed again the next time you follow a link. Here is why:

You have to differentiate between a render and a redirect_to because a flash message is only deleted after a redirect. If you want a message to be seen in the next request after a redirect, use flash[]. If you want a message to be seen in the current request, use[].

Workaround for the lazy

If you cannot be bothered to decide which flash hash to use, or if the flash is set by a piece of code outside your control, you can also change your layout displaying the flash so it deletes the flash message once displayed:

- flash.slice(:notice, :message, :error, :success, :warning, :failure).each do |level, value| - if value.present? %div{:class => "flash #{h level}"} = h value - flash[level] = nil # set to nil in case the flash was set without a following redirect

Does your version of Ruby on Rails still receive security updates?
Rails LTS provides security patches for unsupported versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2).

Owner of this card:

Dominik Schöler
Last edit:
over 9 years ago
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Dominik Schöler to makandra dev
This website uses short-lived cookies to improve usability.
Accept or learn more