【问题标题】:Selectively turning off Devise's flash notices in Rails 3在 Rails 3 中选择性地关闭 Devise 的 Flash 通知
【发布时间】:2011-03-12 07:51:01
【问题描述】:

Devise 身份验证框架在任何地方都使用 Flash 通知。这使得与应用程序集成变得容易,但有时会导致用户体验不佳。

我想知道有什么简单的方法可以在我的 Rails 3 应用程序中选择性地关闭一些 Devise 闪存通知。特别是,我想摆脱明显的 signed_in 和 signed_out 闪烁。

一些搜索建议将会话控制器子类化或使用something like this,但我无法找到任何简单的解决方案。

【问题讨论】:

    标签: ruby-on-rails-3 authentication devise


    【解决方案1】:

    您可以使用 devise 支持的 I18n 后端自定义您的 devise flash 消息。如果您为特定键设置 nothing,则不会显示空的 flash 消息,例如 sign_in 和 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。

    您不应该删除密钥,否则您会收到错误消息。 要不显示空的 flash 消息,您应该在视图中进行简单的检查(例如使用 haml):

    - unless notice.blank? && alert.blank?
      #flash
        .wrapper
          - unless notice.blank?
            %p.notice= notice
          - unless alert.blank?
            %p.alert= alert
    

    【讨论】:

    • 这会导致错误消息:翻译缺失:en.devise.sessions.user.signed_out
    • 你删除了这个键吗?在我更新答案时尝试使用引号设置空字符串。
    • Voldy,如果我将翻译设置为 nothing 或空字符串,那么我会得到一个空的 flash。如果我删除密钥,我会收到错误消息。
    • @Sim,只需使用blank? 进行检查。我用一个例子更新了我的答案。
    • 我想我在某处读到过,在除非情况下使用多个逻辑条件不是很好的做法。但是很好的解决方案。它帮助了我。
    【解决方案2】:

    隐藏空的 flash 消息的更好方法:如果您的消息位于具有“notice”或“error”类的 div 中,CSS3 可以让您拥有这样的样式:

    .notice:empty {
      display: none;
    }
    

    这很好,因为您可以始终显示 flash div,并且它只会在有东西时显示。我使用它来更新来自 ajax 调用的响应中的 flash 消息,否则不会更新 flash 消息,因为不涉及页面重新加载。它使由 ajax 调用产生的消息具有一致的外观。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-16
      • 1970-01-01
      相关资源
      最近更新 更多