简体   繁体   English

Rails 4-用户按下浏览器后退按钮时更新特定数据

[英]Rails 4 - Update a specific data when user press browser back button

I have a very "basic" issue, simple to explain but hard to solve. 我有一个非常“基本”的问题,易于解释但很难解决。

On my homepage, I show a user the number of times he saw the page we can call Alpha. 在我的主页上,我向用户显示了他看到我们可以称为Alpha的页面的次数。

<div>
<%= link_to "page alpha",
            page_alpha_path(deal) %> <br/>
You have seen this page alpha #{deal.number_of_times_viewed} times
</div>

It works. 有用。 but I 'd like to be able to do the following for example: 但我希望能够执行以下操作:

  • user goes on homepage and sees the link and can read "You have seen this page alpha 4 times times already" 用户进入主页并看到链接,可以阅读“您已经4次浏览此页面alpha了”
  • he clicks on the alpha page link and arrive and alpha page 他单击alpha页面链接并到达alpha页面
  • he presses Chrome's 'Back button' and sees now 'You have seen this page alpha 5 times 他按下了Chrome的“后退按钮”,现在看到的是“您已经5次浏览此页面alpha

As you see even on 'press back' the page has reloaded the number of times, it has triggered a call to the database table to recheck the number of times he saw the page. 如您所见,即使在“按回”时,页面也已重新加载次数,它触发了对数据库表的调用以重新检查他看到该页面的次数。

Today unfortunately, whatever I tried, he still sees " 4 times" 不幸的是,今天,无论我如何尝试,他仍然看到“ 4次”

How to do it? 怎么做? Can I do it in pure Ruby/Rails or might I need some javascript or gon Watch ? 我可以使用纯Ruby / Rails进行操作,还是需要一些javascript或gon Watch? or maybe some Rails UJS? 还是一些Rails UJS?

try to clear your response cache like this using a call_back . 尝试使用call_back这样清除响应缓存。 it will work. 它会工作。

class ApplicationController < ActionController::Base

    before_filter :set_cache_headers

      private

      def set_cache_headers
        response.headers["Cache-Control"] = "no-cache, no-store, max-age=0, must-revalidate"
        response.headers["Pragma"] = "no-cache"
        response.headers["Expires"] = "Fri, 01 Jan 1990 00:00:00 GMT"
      end
    end

Try using this code: 尝试使用以下代码:

class ApplicationController < ActionController::Base
before_filter :pages_count

def pages_count
  if current_user
   current_user.update_attributes(:pages_count=>current_user.pages_count+ 1)
   end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当在rails中按下浏览器的后退按钮时,在注销后进入登录页面 - Be on login page after Logout when press back button of Browser in rails 如果用户在特定的导轨视图上输入任何 URL,如何防止浏览器的后退按钮并保持同一页面? - How to prevent browser's back button and stay same page if user entered any URL on specific rails view? rails 4-仅在离开网页时使用浏览器的后退按钮更新属性 - rails 4 - update attribute with browser's back button only when leaving a webpage Rails-用户单击提交按钮时如何更新表中的数据 - Rails - How update data in table when user clicks on submit button Rails:当用户通过浏览器后退按钮离开编辑视图时运行一个方法 - Rails: Run a method when user left the edit view through the browser back button 是否可以捕获浏览器的后退按钮按下 - Is it possible to capture browser back button press 如果用户在Rails上点击了ruby中的浏览器后退按钮,如何将其重定向到? - How to redirect_to if the user hits the browser back button in ruby on rails? Rails,在浏览器中的“后退”按钮上注销 - Rails, Logout on Back Button in Browser Rails:控制浏览器后退按钮 - Rails: Controlling browser back button 在 Rails 5 中单击浏览器上的后退按钮时,会复制带有 Select2 的表单 - Forms with Select2 are duplicated when clicking back button on browser in Rails 5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM