开发者

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).

开发者_如何学Go

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!


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


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.


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


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


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.


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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜