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

前端 未结 8 891
囚心锁ツ
囚心锁ツ 2020-12-13 12:38

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 jus

相关标签:
8条回答
  • 2020-12-13 13: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

    0 讨论(0)
  • 2020-12-13 13:17

    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.

    0 讨论(0)
  • 2020-12-13 13:20

    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;
    }
    
    0 讨论(0)
  • 2020-12-13 13:22

    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.

    0 讨论(0)
  • 2020-12-13 13:24

    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
    
    0 讨论(0)
  • 2020-12-13 13:26

    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.

    0 讨论(0)
提交回复
热议问题