使用 Rails 和 Turbo 链接时防止 Flash 加载到不需要的页面上

Preventing flash from loading on undesired pages while using Rails and Turbo Links

我有一个问题,即我的 Flash 消息在我不希望它出现的页面上挥之不去。

我正在使用 Rails 4 和 turbo links。

虽然我非常喜欢 turbo links,但我认为这可能是问题所在。

在我的 rails 控制器中,我按如下方式设置欲望闪光灯:

def show
     if item.status == "In Progress" || item.status == "draft"
            flash[:alert] = "Please remember: the status of this text is draft. You have been granted access through the generosity of the editor. Please use the comments to help make suggestions or corrections."
     end
 end

但是,当我单击 link 并且 turbo links 响应地加载新页面时,flash 再次出现。

关于如何在继续使用 turbo 的同时防止这种情况的任何想法links

您可以在应用程序控制器中清除 before_action 中的闪现消息 在application_controller.rb

before_action :clear_flash

def clear_flash
  flash.discard
end

或者只需将您的代码更改为:

def show
 if item.status == "In Progress" || item.status == "draft"
        flash.now[:alert] = "Please remember: the status of this text is draft. You have been granted access through the generosity of the editor. Please use the comments to help make suggestions or corrections."
 end    
end