我正在编写一个简单的聊天,我需要列出在线用户。我不使用devise
对于身份验证,有一个自定义user
通过验证的模型omniauth
.
user.rb
class User < ActiveRecord::Base
has_many :messages, dependent: :delete_all
class << self
def from_omniauth(auth)
provider = auth.provider
uid = auth.uid
info = auth.info.symbolize_keys!
user = User.find_or_initialize_by(uid: uid, provider: provider)
user.name = info.name
user.avatar_url = info.image
user.profile_url = info.urls.send(provider.capitalize.to_sym)
user.save!
user
end
end
end
应用程序控制器.rb
def current_user
@current_user ||= User.find_by(id: cookies[:user_id]) if cookies[:user_id]
end
helper_method :current_user
我尝试以这种方式做到这一点:添加到application_controller.rb
a show_online
method:
def show_online
@users = User.where(status: online)
end
helper_method :online_users
然后添加到视图中:
<%= online_users.each do |user| %>
<ul>
<li><%= user.name %></li>
</ul>
<%end%>
但它抛出异常ActionView::Template::Error (undefined method 'online_users' for #<MessagesController:0x007f52d7f82740>)
源代码 https://github.com/AlexNikolaev94/chatclone.git here
EDIT
对我来说最好的解决方案是我找到的here https://stackoverflow.com/questions/10805479/rails-how-to-check-for-online-users,但我完全不知道如何正确实现它:(但这绝对是我需要的