I bring to mind a little chat, who wrote, and faced the following difficulty - I wanted to deduce users that they are online in a chat, but it doesn’t work. In order to define a user online, I first created a migration in the database and added the last_online attribute: add_last_online_to_users last_online:datetime to the user model. Then I added the online_now method to the User class:

 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 def online_now where("last_online > ?", 15.minutes.ago) end end end 

and in application_controller.rb :

 def show_online @users = User.online_now end 

Also, in the controllers that are responsible for creating a new session and sending a message, I specified the condition for updating the last_online attribute

messages_controller.rb :

 def create respond_to do |format| if current_user @message = current_user.messages.build(message_params) @message.save current_user.update_attributes(last_online: Time.now) format.html{redirect_to root_path} format.js else format.html{redirect_to root_path} format.js {render nothing: true} end end end 

sessions_controller.rb

 def create user = User.from_omniauth(request.env["omniauth.auth"]) cookies[:user_id] = user.id user.update_attributes(last_online: Time.now) end 

and finally in the view:

 <ul> <%= @users.each do |user| %> <li><%= user.name %></li> <% end %> </ul> 

Checked through the User.online_now console - the user that is currently logged in to the chat or sent a message is there, but not in the list.

  • And ... And you update the list in the browser? Well, of course you chose the first project on Rails, chat ... This is not the technology on which to do this. - D-side
  • @D-side of the matter is that after logging in, I redirect to the page where the list, that is, the page with the list is updated. But it is empty: (and about the first project - I just wrote a couple of unpretentious blozhikov, so I wanted something more interesting - AlexNikolaev94
  • Chat on the tracks is more a struggle with technology than creativity :) But your business. Take pry , see that in @users and that in User.online_now . - D-side
  • 1. user.update_attributes(last_online: Time.now) but does this work out at all? Maybe there is an invalid user. In the database, if you look at what is in this field for an authorized user? 2. show_online and where is it called at all? - anoam
  • @anoam 1. executes, in the database it finds the user with the last_online attribute. 2. show_online is called in a common view through @users.each - AlexNikolaev94

1 answer 1

You may not have called the show_online method anywhere, so your @users is empty. On the other hand, then there should be an error for each. Add code in which you call show_online for a specific controller?