显示某人是否正在阅读信息 [英] Showing if someone is reading a post
问题描述
我想显示正在阅读信息的人员列表。
在尝试实现该函数之前,我想问一些建议。我将首先检查在 PostsController
中调用 show
操作的用户;每次一个人查看帖子时,他将被记录在正在阅读帖子的人的列表中。不过,我不知道在离开时,如何从列表中删除此人。是否有方法使用 cookies
或 sessions
通知用户是否离开某个页面?有一点帮助可以帮助我马上开始!
I want to show the list of people who are reading a post.
Before attempting to implement the function, I wanted to ask for some advice. I would start by checking users who invoke the show
action in the PostsController
; every time a person views a post, he will be recorded onto the list of people who are reading the post. However, I am not sure how to remove the person from the list when he navigates away from reading the post. Is there a way to use cookies
or sessions
to notice if the user navigates away from a certain page? A little bit of help would help me get started right away!
我很感激任何建议。谢谢!
I appreciate any advice. Thank you!
推荐答案
更准确
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);
});
}
in post.rb
in 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
这篇关于显示某人是否正在阅读信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!