5

Ok, this is a weird one.

flash[:success] = 'some success message'
redirect_to :controller => 'media', :action => 'index'

The message is being displayed after the redirect, the thing is it also appears one more time after clicking on a link or go to another page in my app (after the first redirect)

mendi
  • 85
  • 6

2 Answers2

7

For flash, first differentiate between a render and a redirect_to because a flash message is only deleted after a redirect. This you have ok.

Then, 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 flash.now[].

See if this helps.

If you're really stuck you can clear it in the view - though you are loading up technical debt with such workaround hacks - but if the clock is ticking right now:

- 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 outside of a redirect
Community
  • 1
  • 1
Michael Durrant
  • 93,410
  • 97
  • 333
  • 497
0

Also, you must pay attention not to include flash messages on your view/layouts/application.html.erb file. It may happend :)

miksiii
  • 2,426
  • 26
  • 22