52

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

Marat_Galiev
  • 1,251
  • 5
  • 21
  • 31
  • 1
    Duplicates this question http://stackoverflow.com/questions/5281365/selectively-turning-off-devises-flash-notices-in-rails-3 – Voldy Apr 23 '11 at 11:19

6 Answers6

78

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 %>
Michelle Tilley
  • 157,729
  • 40
  • 374
  • 311
38

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

acrogenesis
  • 937
  • 9
  • 21
  • 5
    in Rails 4 and Devise 3.4.1, i had to add this to the routes: devise_for :users, controllers: {sessions: "sessions"} – Matthew Apr 24 '15 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 ;-)

jpheos
  • 448
  • 5
  • 12
  • does not work with rails 2, but we are in 2018 now :). For Rails3, use `after_filter` instead of `after_action` – jpheos Apr 10 '18 at 07: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

Manuel Pedrera
  • 5,347
  • 2
  • 30
  • 47
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' }

zhisme
  • 2,368
  • 2
  • 19
  • 28
-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).

Tony Daly
  • 115
  • 6
  • 1
    It looks like an answer for another question. You can't disable flash messages with that. See Brandon's answer. – Voldy Apr 23 '11 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. – Benjamin Atkin Oct 13 '12 at 01:15