benoitr
benoitr

Reputation: 6105

Devise/Rails - How to remove a particular flash message? (Signed in Successfully)

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!

Upvotes: 45

Views: 20236

Answers (8)

andrewcockerham
andrewcockerham

Reputation: 2706

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

Upvotes: 12

shiva kumar
shiva kumar

Reputation: 11424

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.

Upvotes: 0

omarwaleed
omarwaleed

Reputation: 681

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.

Upvotes: 3

Sunny
Sunny

Reputation: 6083

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.

Upvotes: 8

fhidalgo
fhidalgo

Reputation: 129

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

Upvotes: 3

Sean Schofield
Sean Schofield

Reputation: 597

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;
}

Upvotes: 11

benoitr
benoitr

Reputation: 6105

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.

Upvotes: 44

shingara
shingara

Reputation: 46914

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

Upvotes: 69

Related Questions