[英]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: 但我希望能够执行以下操作:
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.