I'm writing a simple chat, and I need to list out users online. I don't use devise
for authentication, there's a custom user
model which authenticates via 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
application_controller.rb
def current_user
@current_user ||= User.find_by(id: cookies[:user_id]) if cookies[:user_id]
end
helper_method :current_user
I tried to do that in such way: add to application_controller.rb
a show_online
method:
def show_online
@users = User.where(status: online)
end
helper_method :online_users
and then add to a view:
<%= online_users.each do |user| %>
<ul>
<li><%= user.name %></li>
</ul>
<%end%>
but it throws an exception ActionView::Template::Error (undefined method 'online_users' for #<MessagesController:0x007f52d7f82740>)
source code here
EDIT
the best solution as for me I found here, but I completely don't get how to implement it correctly :( But that's definitely what I need