单击 RAILS 中的后退按钮,禁用 Flash 消息而不禁用缓存 [英] Disabling Flash message without disabling cache on click on back button In RAILS

查看:39
本文介绍了单击 RAILS 中的后退按钮,禁用 Flash 消息而不禁用缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用 rails 编写的应用程序.我有闪现消息显示用户说他成功登录或创建或更新了他的个人资料.一旦用户登录,就会显示一条闪现消息(欢迎"),并且用户导航到不同的页面.如果用户单击返回"按钮,则会再次显示闪现消息(欢迎").

我不想禁用缓存,因为它会影响性能.

解决这种情况的任何想法都会有所帮助.

谢谢.

解决方案

这是针对通过搜索引擎提出此问题的人.

问题

当用户点击后退按钮时,不会向服务器发出新的请求,而是显示浏览器的缓存副本.但是,在第一次请求期间,浏览器已经缓存了 flash 消息,单击后退按钮时,flash 消息再次显示.

解决方案

解决此问题的一种方法是使用浏览器的 localStorage 或 sessionStorage.

更改您的代码,以便通过 javascript 显示或隐藏 Flash 消息.当显示 flash 消息时,将该事实存储在浏览器的 sessionStorage 中.下一次,检查闪信是否已经显示,如果显示,不要再显示.

我更喜欢使用 sessionStorage 而不是 localStorage,因为 sessionStorage 在关闭浏览器时会被清理.如果您有太多的 Flash 消息,这会很有帮助.否则,您将不得不编写删除旧条目的逻辑

# app/views/layouts/application.html.erb - 或者你的 flash 信息所在的任何地方...<%flash_name_mappings = {通知" =>成功",错误" =>危险",警报" =>警告"}%><% flash.each do |name, msg|%><% if msg.is_a?(String) %><% flash_token = Time.current.to_s(:number) # 或使用任何其他随机令牌生成器,例如 Devise.friendly_token %><div id="flash-<%= flash_token %>"style="display:none" class="alert alert-<%= flash_name_mappings[name] || name %>"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button><%= content_tag :div, msg.html_safe, id: "flash_#{name}" %>

<script type="text/javascript">$(document).ready(function(){if (typeof(Storage) !== "undefined") {//localStorage/sessionStorage 的代码.if(sessionStorage["flash-<%= flash_token %>"] != "shown"){$("#flash-<%= flash_token %>").show();sessionStorage["flash-<%= flash_token %>"] = "shown";}} 别的 {//对不起!没有网络存储支持..$("#flash-<%= flash_token %>").show();}}<%结束%><%结束%>...

I have an application written in rails. I have flash messages displaying the user saying he logged in successfully or created or updated his profile. Once the user logs in a flash message("Welcome") is displayed and the user navigates to a different page. If the user clicks BACK button the flash message ("Welcome") is displayed again.

I dont want to disable Cache as it will affect the performance.

Any ideas to tackle this situation will be helpfull.

Thanks.

解决方案

This is for people coming to this question via search engines.

Problem

When the user clicks on the back button, a fresh request is not made to the server, rather the browser's cached copy is displayed. However, during the first request, the browser has already cached the flash message, and on clicking the back button, the flash message gets displayed again.

Solution

A way to get around this is to use the browser's localStorage or sessionStorage.

Change your code so that the flash messages are shown or hidden via javascript. When the flash message is shown, store that fact in the browser's sessionStorage. Next time, check if the flash message has already been shown and if so, do not show it again.

I prefer to use sessionStorage instead of localStorage as sessionStorage gets cleaned up on closing the browser. It is helpful if you have too many flash messages. Otherwise, you will have to write the logic for removing older entries

# app/views/layouts/application.html.erb - or wherever your flash messages reside

...
<% 
  flash_name_mappings = {
    "notice" => "success",
    "error" => "danger",
    "alert" => "warning"
  }
%>

<% flash.each do |name, msg| %>
  <% if msg.is_a?(String) %>
    <% flash_token = Time.current.to_s(:number) # or use any other random token generator, like Devise.friendly_token %>

    <div id="flash-<%= flash_token %>" style="display:none" class="alert alert-<%= flash_name_mappings[name] || name  %>">
      <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
      <%= content_tag :div, msg.html_safe, id: "flash_#{name}" %>
    </div>

    <script type="text/javascript">
      $(document).ready(function(){
        if (typeof(Storage) !== "undefined") {
          // Code for localStorage/sessionStorage.
          if(sessionStorage["flash-<%= flash_token %>"] != "shown"){
            $("#flash-<%= flash_token %>").show();
            sessionStorage["flash-<%= flash_token %>"] = "shown";
          }
        } else {
          // Sorry! No Web Storage support..
          $("#flash-<%= flash_token %>").show();
        }
      }
    </script>
  <% end %>
<% end %>

...

这篇关于单击 RAILS 中的后退按钮,禁用 Flash 消息而不禁用缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆