Selectively turning off Devise's flash notices in Rails 3

后端 未结 2 1505
挽巷
挽巷 2021-02-05 05:46

The Devise authentication framework uses flash notices everywhere. This makes it easy to integrate with apps but it leads to poor user experience sometimes.

I am wonde

相关标签:
2条回答
  • 2021-02-05 06:14

    A better way to hide empty flash messages: if your message is in a div with class "notice" or "error" CSS3 lets you have a style like this:

    .notice:empty {
      display: none;
    }
    

    Which is nice because you can then always display the flash div and it'll only show up when there's something there. I use this to update flash messages in responses from ajax calls which otherwise wouldn't update flash messages because there's no page re-load involved. It gives a consistent look to messages that come as a result of ajax calls.

    0 讨论(0)
  • 2021-02-05 06:16

    You can customize your devise flash messages with I18n backend which devise supports. If you set nothing for particular key, the empty flash message won't be shown, for example for sign_in and sign_out:

    en:
      devise:
        failure:
          unauthenticated: 'You need to sign in or sign up before continuing.'
          unconfirmed: 'You have to confirm your account before continuing.'
          locked: 'Your account is locked.'
          invalid: 'Invalid email or password.'
          invalid_token: 'Invalid authentication token.'
          timeout: 'Your session expired, please sign in again to continue.'
          inactive: 'Your account was not activated yet.'
        sessions:
          signed_in: ""
          signed_out: ""
    

    UPD.

    You should not remove the key otherwise you will get an error. To not display empty flash messages you should do simple check in the view (for ex. with haml):

    - unless notice.blank? && alert.blank?
      #flash
        .wrapper
          - unless notice.blank?
            %p.notice= notice
          - unless alert.blank?
            %p.alert= alert
    
    0 讨论(0)
提交回复
热议问题