55

How I can disable all Devise gem flash messages ("successfully signed in","you logged out")? Thanks.

1

7 Answers 7

79

Probably the easiest way to do this is to

  1. Define each message as a blank string
  2. Check the length of the string before you show a flash message.

In your devise.en.yml file, specify each message as empty:

en:
  errors:
    messages:
      not_found: ''
      already_confirmed: ''
      not_locked: ''

etc. Next, in your layout, check for blank flash strings before you output them.

<% flash.each do |key, value| %>
  <%= content_tag :div, value, :class => "flash #{key}" unless value.blank? %>
<% end %>
3
  • 8
    Very elegant! For my devise (2.1.0) just setting the the string to '' did the trick without checking for blank? Commented May 30, 2013 at 13:59
  • what about session message etc, not working like this Commented Jun 26, 2018 at 14:42
  • Would this continue to work if the locale was not en? Commented Apr 29, 2023 at 6:17
40

An answer better suited for me was to override the Devise Session Controller like this

class SessionsController < Devise::SessionsController

  # POST /resource/sign_in
  def create
    super
    flash.delete(:notice)
  end

  # DELETE /resource/sign_out
  def destroy
    super
    flash.delete(:notice)
  end

end

This safely overrides the create and destroy method removing the flash message

1
  • 5
    in Rails 4 and Devise 3.4.1, i had to add this to the routes: devise_for :users, controllers: {sessions: "sessions"}
    – Matthew
    Commented Apr 24, 2015 at 21:20
8

This work for me:

# app/controllers/users/sessions_controller.rb
class Users::SessionsController < Devise::SessionsController
  after_action :remove_notice, only: [:destroy, :create]

  private

  def remove_notice
    flash.discard(:notice) #http://api.rubyonrails.org/v5.1/classes/ActionDispatch/Flash/FlashHash.html#method-i-discard
  end
end

# add this line in 'config/routes.rb'
devise_for :users, :controllers => { sessions: 'users/sessions' }

I use Users::SessionsController but you can use SessionsController, I have just one devise model in this example.

I use flash.discard(:notice) but you can use flash.discard to remove others types in same time. (method discard exist since rails 3.0)

I prefer this approach, because it's not the role of the view to check if your flash message if blank. If you have a flash message, print it! If you don't want, so don't create flash message ;-)

1
  • does not work with rails 2, but we are in 2018 now :). For Rails3, use after_filter instead of after_action
    – jpheos
    Commented Apr 10, 2018 at 7:54
7

I've been able to disable them in a given controller by overriding is_flashing_format?:

def is_flashing_format?
  false
end

I'm using Devise 3.5.6

2

For Rails 5.0.6 this code will work.

app/controllers/sessions_controller.rb

class SessionsController < Devise::SessionsController

  def new
    flash.clear
    super
  end
end

Do not forget the routes.

config/routes.rb

devise_for :users, controllers: { sessions: 'sessions' }

0

As of today, just leaving an empty string disable the toast entirelly. This works :

  sessions:
  signed_in: ""
  signed_out: ""
  already_signed_out: ""
-4

Devise includes a handy generator to copy all the views into your project:

rails generate devise:views

This way you can edit the views yourself and decide what you want to keep or throw away (flash messages).

2
  • 1
    It looks like an answer for another question. You can't disable flash messages with that. See Brandon's answer.
    – Voldy
    Commented Apr 23, 2011 at 11:17
  • 1
    You can if you display your flash messages in each view, by deleting it. But it seems this question assumes that they're in the layout. Commented Oct 13, 2012 at 1:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.