Ruby on rails 用户的最后一个联机属性仍然为“nil”`

Ruby on rails 用户的最后一个联机属性仍然为“nil”`,ruby-on-rails,ruby,Ruby On Rails,Ruby,我正在建立一个小型实时聊天室,我一直在网上列出用户名单。我有一个自定义的User模型,为了列出在线用户,我做了一个迁移add_last_online\u to_users last_online:datetime。然后,在myuser.rb中: def show_online @users = User.online_now end 已编辑 class User < ActiveRecord::Base has_many :messages, dependent: :de

我正在建立一个小型实时聊天室,我一直在网上列出用户名单。我有一个自定义的
User
模型,为了列出在线用户,我做了一个迁移
add_last_online\u to_users last_online:datetime
。然后,在my
user.rb中:

def show_online
    @users = User.online_now
end
已编辑

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
会话\u 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
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
在编辑的
消息\u 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
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
最后一个观点是:

<ul>
    <%= @users.each do |user| %>
        <li><%= user.name %></li>
    <% end %>
</ul>    
编辑 但当我在控制台中登录或发送消息后进行检查时-视图中没有列出任何用户,并且当我尝试在控制台上调用
User.online\u now
时,它会为用户返回
未定义的方法“online\u now”

另一次编辑
我已经从
User
类中的
online\u now
方法中删除了
self
,现在没有
未定义的方法
错误,但在我看来仍然没有用户在线:(

似乎您没有在sessions\u controller.rb中保存最后一个\u online属性,只是分配值。请尝试以下操作:

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

在messages\u controller.rb中也一样。尝试将
current\u user.last\u online=Time.now
替换为
current\u user.update\u attributes(last\u online:Time.now)

您似乎没有在sessions\u controller.rb中保存last\u online属性,只是分配值。请尝试以下操作:

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

在messages\u controller.rb中也一样。尝试用
当前用户替换
current\u user.last\u online=Time.now
。更新\u属性(last\u online:Time.now)

因为您已经使用“类”使用户方法成为静态的因为您已经使用“类”使用户方法成为静态的,这很有帮助,谢谢!但是它仍然不会在视图中在线显示用户:(我猜最后一次联机现在被存储到数据库中。
User.online\u now
会在rails控制台中返回用户吗?很抱歉反应太晚!我通过控制台检查,它会为用户返回
未定义的方法“online\u now”。在我的问题中,我提供了整个
User
模型,这很有帮助,谢谢!但它仍然没有显示给我们。)视图中的ers online:(我猜最后一次联机现在被存储到数据库中。
User.online\u now
在rails控制台中返回用户吗?很抱歉反应太晚!我通过控制台检查,它为用户返回
未定义的方法“online\u now”。在我的问题中,我提供了整个
用户
模型谢谢!现在在控制台中我找到了
用户
现在谁在线
,但视图中仍然没有用户在线:((谢谢!现在我在控制台中找到了
用户
现在谁在线
,但视图中仍然没有用户在线:(((