【问题标题】:Message "true" flashes on main page after session timeout会话超时后,消息“true”在主页上闪烁
【发布时间】:2014-12-18 03:08:06
【问题描述】:

我有一个 Rails4 应用程序,当会话由于不活动而超时时,我试图闪现一条自定义消息。

我通过在devise.rb 中配置timeout_in 设置来做到这一点:

Devise.setup do |config|
  ...
  config.timeout_in = 30.minutes
  ...
end

并向我的应用程序控制器添加自定义救援:

rescue_from CanCan::AccessDenied do |exception|
  if user_signed_in?
    flash.now.alert = exception.message
    render text: '', layout: true, status: 403
  else
    redirect_to new_user_session_path, notice: flash[:alert] || "You must login first"
  end
end

一切似乎都工作正常...当会话超时时,flash[:alert] 已经有正确的消息,所以我只使用它,当用户尝试访问资源而不先登录时,“你必须登录第一个”消息被返回。

这是 main.html.haml 页面中呈现这些警报的代码:

.container
  .main-content
    #flash
      - flash.each do |type, msg|
        %div{class: ('alert alert-dismissable fade in')}
          %button.close{data: {dismiss: :alert}} ×
          = msg
    = yield

问题是,我偶尔会看到会话超时消息正下方出现一个带有文本“True”的闪光:

我不知道它是从哪里来的。似乎有些东西正在创建具有该值的 Flash 消息。我想知道我做错了什么,或者是否有更好的方法来显示会话超时消息。有什么想法吗?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 devise timeout cancan


    【解决方案1】:

    我知道这是一个迟到的回复,但这就是我所做的。

    flash.each do |type, message|
      unless type.to_s == 'timedout'
      <!-- Carry on printing messages -->
    

    【讨论】:

      【解决方案2】:

      尝试输出每个 flash 的 type - 也许 Devise 或其他东西正在将 flash 用于其他事情,而不仅仅是消息。 Read more about using the flash for other things than messages.

      我不会遍历所有 Flash 键来呈现它们——我会明确地只呈现那些专用于消息的键(通常是 :notice:alert)。

      【讨论】:

      • 谢谢亨利克。我不确定让flash 控制应用程序状态的决定是否是一个好的设计决定,但这就是它的工作原理,我们对此无能为力。
      【解决方案3】:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-10-19
        • 2022-01-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-24
        相关资源
        最近更新 更多