显示是否有人正在阅读post

我想显示正在阅读post的人员列表。 在尝试实现该function之前,我想要一些建议。 我首先检查在PostsController调用show动作的PostsController ; 每当一个人观看post时,他都会被记录在正在阅读post的人员名单上。 但是,当他离开阅读post时,我不确定如何从列表中删除此人。 有没有办法使用cookiessessions来通知用户是否离开某个页面? 一点帮助可以帮助我马上开始!

我很感激任何建议。 谢谢!

更准确

在routes.rb中

 post "/posts/:id/being_read" => "posts#being_read", :defaults => { :format => "json"} 

在控制器中

 PostsController < ApplicationController def being_read current_user && @post.being_read_by current_user head :ok end end 

在节目页面上

 function beingRead(){ $.post("/posts/<%=@post.id%>/being_read", function(data) { setTimeout(beingRead,10000); }); } 

在post.rb

 def being_read_by user=nil # making use of rails low level cache readers_ids = Rails.cache.fetch("Post #{id} being read", :expires_in => 5.minutes) || [] readers_ids = Rails.cache.fetch("Post #{id} being read", :expires_in => 5.minutes) (readers_ids << user.id) if user readers_ids end 

通常,使用超时系统。

我们可以存储用户阅读post的日期,而不是存储布尔“正在阅读post”。

不,您可以轻松设置超时(例如5分钟)以了解谁正在阅读post。 这是近似值,但这几乎是现实的。

好处 :

  • 您可以在需要时(使用cron,每天或其他内容)删除完成的日期,使用单个查询而不是每个视图一个查询。
  • 您解决关闭其浏览器的用户的问题(当没有打开下一页时)