ruby on rails - Showing if someone is reading a post -


i want show list of people reading post. before attempting implement function, wanted ask advice. start checking users invoke show action in postscontroller; every time person views post, recorded onto list of people reading post. however, not sure how remove person list when navigates away reading post. there way use cookies or sessions notice if user navigates away page? little bit of me started right away!

i appreciate advice. thank you!

to more accurate

in routes.rb

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

in controller

postscontroller < applicationcontroller   def being_read     current_user && @post.being_read_by current_user     head :ok   end end 

on show page

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

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 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -