2

I have a custom error controller to show dynamic error pages (for 404, 422,500, etc), everything works fine but I cannot delete a flash message (according to http://blog.plataformatec.com.br/2012/01/my-five-favorite-hidden-features-in-rails-3-2/).

Is there a way to delete the flash inside my custom error controller?

akmozo
  • 9,829
  • 3
  • 28
  • 44
John
  • 588
  • 1
  • 6
  • 22
  • Can you show us controller file ? –  Mar 09 '15 at 13:27
  • Here you got: https://gist.github.com/xxswingxx/b82296a69377879f666a It's pretty simple. If I try to `flash[:something] = nil` and then I reload the page, the flash value will still be there. – John Mar 09 '15 at 13:35

1 Answers1

11

Using Rails 4:

class ErrorController < ActionController::Base
   before_action { flash.clear }

   # ...
end

Reference: http://api.rubyonrails.org/classes/ActionDispatch/Flash/FlashHash.html#method-i-clear

Rodrigo
  • 5,435
  • 5
  • 42
  • 78
  • Tried, doesn't work. The problem is I'm returning error status as this is rendering custom error pages, so flash, session variables, etc somehow are preserved by default by rails and it doesn't allow modifications. – John Mar 09 '15 at 14:16
  • Ok. You can't remove the `flash` render code form the error pages? – Rodrigo Mar 09 '15 at 14:17
  • I can delete it but only locally. If I reload the page and reach the error controller again, the flash message will still be there without setting it again like if the deletion had no effect. – John Mar 10 '15 at 09:47