如何显示在线用户 [英] How to show online users

查看:53
本文介绍了如何显示在线用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个简单的聊天,我需要列出在线用户.我不使用 devise 进行身份验证,有一个自定义的 user 模型通过 omniauth 进行身份验证.

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

我尝试这样做:在 application_controller.rb 中添加一个 show_online 方法:

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

然后添加到视图:

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

但它抛出一个异常 ActionView::Template::Error (undefined method 'online_users' for #<MessagesController:0x007f52d7f82740>)

源代码在这里

编辑

对我来说最好的解决方案是在这里,但是我完全不明白如何正确实现它:(但这绝对是我需要的

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

推荐答案

您遇到的错误可以通过以下两种方式之一解决.
您可以使用辅助方法并从您的视图中调用它们,就像您想做的那样.
或者,您可以完全避免使用它们,而只需在加载视图时从当前正在使用的任何方法调用 show_online 方法.如果您要去show,它将是show 方法,依此类推.

The error you get can be solved in one of two ways.
You can use helper methods and call them from your view, as it seems you want to do.
Or, you can avoid using them completely and just call the show_online method from whichever method is currently being when the view is loaded. If you're going to the show, it'll be the show method, and so on and so on.

您自己的答案确实使用第一种方法正确修复了错误,但我推荐这种方式.

Your own answer does correctly fix the errors using the first method, but I recommend this way.

需要做什么来实施这些修复:

What needs to be done to implement these fixes:

  • 在加载 new 时调用 show_online,以便视图可以访问 @users 变量.我们可以通过 before_action

  • Call show_online when the new is being loaded so that view can access the @users variable. We can do that with before_action

在视图中,您有一个循环遍历 online_users,但它应该遍历 @users

In the view, you have a loop which iterates over online_users, but it should iterate over @users

在视图中的同一个循环中,您有一个简单的语法错误.第一行以 <%= 开头,但应该以 <% 开头,不带 =.无论您以何种方式编写代码,都应该更改此设置.

In the that same loop in the view, you have a simple syntax error. The first line starts with <%= but should start with <% without the =. This should be changed no matter what way you write the code.

application_controller.rb

#put this line at the top of the controller, just below the line ApplicationController:: .....
before_action :show_online, only: [:new]

def show_online
    @users = User.where(online: true)

视图文件

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

为什么采用这种方法?

  • 做一种方法来获取在线用户意味着逻辑只有一处
  • 将您的逻辑/代码集中在一处意味着您永远不会重复自己,并且您知道如果出现问题该去哪里查看
  • 使用 before_action 意味着除非需要,否则不会进行调用
  • 如果您稍后添加需要获取在线用户列表的页面,您只需将这些添加到此处括号中的方法列表中:only: [:new]
  • 在在视图或控制器中放置逻辑之间的选择时,正确的答案几乎总是控制器
  • 这篇关于如何显示在线用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆