45

Using Devise, I would like to know if there is a way to remove a particular flash message? (Signed in Successfully).

I care about other msg in the view, so It is just for the signed in and the signed out one. Did I have to overwrite the controller or there is another way?

Thank you!

benoitr
  • 6,025
  • 7
  • 42
  • 67

8 Answers8

68

You just define it to an empty string in your local file. In this case you can see nothing.

shingara
  • 46,608
  • 11
  • 99
  • 105
44

Ok!

As Shingara said I define an empty string in devise.en.yml

sessions:
  signed_in: ''

and I also change a bit the following line (provided by nifty-generators):

<% flash.each do |name, msg| %>
  <%= content_tag :div, msg, :id => "flash" if msg.length > 0 %>
<% end %>

In that way, my css doesn't appear.

benoitr
  • 6,025
  • 7
  • 42
  • 67
12

Another way is if you override the Devise controller, in the create action, put this code, which deletes the flash message:

class MyDevise::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 was answered in this other SO question. For a blog post on how to override the Devise controller, see my blog post

Community
  • 1
  • 1
andrewcockerham
  • 2,676
  • 3
  • 23
  • 19
11

Empty string in the locale file (as suggested above) but also add the following CSS snippet to hide (instead of monkeying with your flash views)

.flash.alert:empty {
  display: none;
}
Sean Schofield
  • 597
  • 6
  • 9
8

Another flexible way to to this is to unset the notice after the action:

class SessionsController < Devise::SessionsController
  after_action :remove_notice, only: :destroy

  private

  def remove_notice
    flash[:notice] = nil
  end
end

With this solution you can add conditions to removing or not the notice.

Sunny
  • 5,825
  • 2
  • 31
  • 41
3

From my point of view I dont see the point in emptying a string translation, when you can easily modify how the controller is working. I guess this way is much more correct and satisfying.

A better answer could be to override destroy method in sessionController.

Just creates a file placed in: app/controllers/sessions_controller.rb

As you can see we comment the line creating the flash message.

class SessionsController < Devise::SessionsController 
  # DELETE /resource/sign_out
  def destroy
    signed_out = (Devise.sign_out_all_scopes ? sign_out : sign_out(resource_name))
    #set_flash_message :notice, :signed_out if signed_out && is_flashing_format?
    yield if block_given?
    respond_to_on_destroy
  end
end
fhidalgo
  • 129
  • 5
  • 1
    You might not want to do that because you're monkey patching. If a bug fix is made then you're losing the fix. By setting something you don't care about to an empty string and making sure you don't print out empty string messages you're using the system the way it was designed. –  Jul 03 '15 at 18:53
3

I think that devise now understands that if you change the error message in config/locals/devise.en.yml to an empty string it will automatically ignore it. At least that's what worked with me.

omarwaleed
  • 571
  • 7
  • 19
  • For me, on OOTB Rails 7 I had to add `.present?` to my view, like this `<% if flash[:notice].present? %>`. But I prefer this solution to the controller override one. – genkilabs Jun 16 '22 at 17:48
0

You can do this, Kindly change the condition type and flash type accordingly.

flash.delete(:alert) if flash[:alert] == "You need to sign in or sign up before continuing." @drivers = params[:keyword].blank? ? [] : Driver.find(params[:keyword])

You can do it in before filter.

shiva kumar
  • 11,294
  • 4
  • 23
  • 28