3

Using rails with Devise, anytime you sign in or signout devise spits a Flash Notice on the page which doesn't seem necessary from a UI perspective, it's usually pretty obvious, right?

Is there a way to turn off flash notices in Devise for Sign In and Sign Outs?

Thanks

AnApprentice
  • 108,152
  • 195
  • 629
  • 1,012

3 Answers3

7

Just try adding this code in your sessions_controller.rb.

class SessionsController < Devise::SessionsController
  after_action :clear_sign_signout_flash, :only => [:create, :destroy]
protected
  def clear_sign_signout_flash
    if flash.key?(:notice)
      flash.delete(:notice)
    end
  end
end

Hope this helps :-)

Chloe
  • 25,162
  • 40
  • 190
  • 357
Manoj
  • 81
  • 1
  • 7
  • 1
    Refactor: flash.delete(:notice) if flash.keys.include?(:notice) – dennis Jan 20 '14 at 15:20
  • You have to create the controller in the correct location, and you have to modify the routes to use the new controller. See https://github.com/plataformatec/devise#configuring-controllers – Chloe Sep 22 '17 at 23:09
1

Devise has one way of customizing this type of behavior: overriding controllers. Open Devise source code, find app/controllers/sessions_controller and copy it to your application. Devise will start using your own controller rather than its own. From there you can easily modify flash messages.

Max Chernyak
  • 37,015
  • 6
  • 38
  • 43
  • Thanks. I'm trying that but can't figure out how to update the routes.rb file. Are you familar? This isn't working "devise_for :users, :controllers => {:invitations => "invitations", :sessions => "sessions"}" it makes it so the methods in those controllers can no longer find the views in devise. – AnApprentice Nov 10 '10 at 04:34
  • You don't actually need to do anything in routes except `devise_for :users`. From here you have 2 choices. Either create a `class SessionsController < Devise::SessionsController`, and then override some methods that Devise provides using your own flash messages (see the Devise::SessionsController here: https://github.com/plataformatec/devise/blob/master/app/controllers/devise/sessions_controller.rb). Second choice is to copy that whole thing I linked into controllers/sessions_controller.rb, only replace Devise::SessionsController with SessionsController, and modify stuff anyhow you like. – Max Chernyak Nov 10 '10 at 06:56
  • I consider this a mis-feature in Devise. I've forked the repo and fixed it. See if this fixes the problem: https://github.com/sxross/devise – Steve Ross Nov 10 '10 at 19:06
0

You can go to the localization file and customize any flash messages, even if to specify empty strings. Look in the documentation under i8n and you'll see how it's done.

Steve Ross
  • 4,134
  • 1
  • 28
  • 40
  • Thanks Steve but I don't want to customize, I want to prevent them from being set. Is setting an empty string the hack to get that to work? – AnApprentice Nov 10 '10 at 00:02
  • Strange, I removed it "signed_in: 'Signed in successfully.'" and it still showed up. – AnApprentice Nov 10 '10 at 00:04
  • And setting the string to blank shows an empty flash, which is very bad :) – AnApprentice Nov 10 '10 at 00:05
  • I would refer you to the source code for `lib/controllers/internal_helpers.rb` and look at `set_flash_message`. It's protected, so presumably you "shouldn't" clobber it. But... it's Ruby, so you can open it back up and make it ignore certain messages. – Steve Ross Nov 10 '10 at 00:30
  • Potentially more problematic, Devise (as of 2.1.2) uses the flash messages to store internal states. This can occasionally cause flashes that just say "true". You should run a is_a?(String) check on all your flash messages before displaying them. If you do this, you can also just as well run a .present? check on them, and set them to empty your yml file. https://github.com/plataformatec/devise/issues/1777 – Gabe Martin-Dempesy Oct 31 '12 at 19:29