Ruby on rails 使用会话数据找出谁在线

Ruby on rails 使用会话数据找出谁在线,ruby-on-rails,session,Ruby On Rails,Session,我希望能够找出谁登录了我的web应用程序,并能够打印出登录用户的列表。我还希望能够打印出谁正在查看应用程序的某个部分,例如聊天室,这样我就可以打印出聊天用户 目前,我只有: session[:role_id] = @role_id 当有人登录应用程序时。每次用户向服务器发出请求时,您都可以使用before\u filter捕捉用户。使用当前\u用户的更新的\u at属性或您所做的任何其他操作都可以调用获取登录的\u用户 使用before_过滤器的一个简单示例: 另见: 如果您将会话数据存储在数

我希望能够找出谁登录了我的web应用程序,并能够打印出登录用户的列表。我还希望能够打印出谁正在查看应用程序的某个部分,例如聊天室,这样我就可以打印出聊天用户

目前,我只有:

session[:role_id] = @role_id
当有人登录应用程序时。

每次用户向服务器发出请求时,您都可以使用before\u filter捕捉用户。使用当前\u用户的更新的\u at属性或您所做的任何其他操作都可以调用获取登录的\u用户

使用before_过滤器的一个简单示例:

另见:


如果您将会话数据存储在数据库中,我想您可以创建一个映射到会话表的模型,并在该表中查询用户列表。如果所有会话数据都存储在cookie中,那么前面的答案可能更理想

class RubyDevelopersController < ChatRoomController

 # every time the request hits the server a call is sent to register the user
 before_filter :register_user

 private

 def register_user

    # the current_user's model is updated with the name  
    # of the chatroom he's hidding in.
    # remember that this call also updates updated_at field of the current_user
    current_user.update_attribute( :current_chatroom, 'ruby_developers' )
 end

 def ative_users_in_ruby_developers_chatroom
  # find all users in the current chatroom that has been updated_at in the last 5 minutes.
  Users.find( :all, {
   :conditions => ["current_chatroom = ? and updated_at > ?",'ruby_developers', 5.minutes.ago ],
   :sort => 'updated_at'
  })
 end
end