Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flash message showing twice when using redirect_to (Rails 2)

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)

like image 386
mendi Avatar asked Nov 16 '11 17:11

mendi


1 Answers

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
like image 111
Michael Durrant Avatar answered Nov 07 '22 14:11

Michael Durrant