Flash message showing twice when using redirect_to (Rails 2)

OK, this is weird.

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

The message will be displayed after the redirect, it also appears one more time after clicking the link or going to another page in my application (after the first redirect)

+4
source share
2 answers

For flash, first distinguish between rendering and redirect_to, because the flash message is only deleted after redirection. You are fine.

Then, if you want the message to appear in the next request after redirecting, use flash []. If you want the message to appear in the current request, use flash.now [].

See if that helps.

If you are really stuck, you can clear it in the view - although you are loading technical debt with such workarounds - but if the clock is ticking correctly 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 
+7
source

In addition, you should pay attention not to include flash messages in the file view / layouts / application.html.erb. It can happen :)

0
source

Source: https://habr.com/ru/post/1381533/


All Articles