Rails 设计,使用 Onliner gem 获取在线用户列表
Rails Devise, using Onliner gem for list of online users
我正在尝试使用在线工具 gem (https://github.com/kbokhonko/onliner) 来显示当前在线用户的列表。但是,当我尝试查看列表应该位于的页面时,我得到 "undefined method `each' for nil:NilClass"。请注意,如果我将 "User.online" 更改为 "User.all",错误消息就会消失,我可以查看页面,所以并不是我的方法在错误的地方或类似的地方,我一定是做错了什么在我的执行中gem。如果任何熟悉此 gem 的人都可以解释我做错了什么,那就太好了!我确定这与我的 def 在线方法设置不正确有关,但我不确定我将如何编写它。
users_controller.rb:
def online
@users = User.online
end
online.html.erb:
<table>
<thead>
<tr>
<th>Users Online</th>
</tr>
</thead>
<tbody>
<% @users.each do |user| %>
<tr>
<td><%= user.email %></td>
<td><%= user.user_type %></td>
<% if user_signed_in? && current_user.user_type == 'admin' %>
<td><%= link_to 'Destroy', user, method: :delete, data: { confirm: 'Are you sure you wish to delete this user?' } %></td>
<% end %>
</tr>
<% end %>
</tbody>
</table>
user.rb:
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :onliner
application_controller.rb:
before_filter { |c| current_user.track unless current_user.nil?}
您做的一切都是正确的,看起来 onliner
gem 中有错误。 User.online
returns nil
而不是您的空数组。我建议你在 gem 回购中打开一个问题。或者使用其他一些 gem.
我正在尝试使用在线工具 gem (https://github.com/kbokhonko/onliner) 来显示当前在线用户的列表。但是,当我尝试查看列表应该位于的页面时,我得到 "undefined method `each' for nil:NilClass"。请注意,如果我将 "User.online" 更改为 "User.all",错误消息就会消失,我可以查看页面,所以并不是我的方法在错误的地方或类似的地方,我一定是做错了什么在我的执行中gem。如果任何熟悉此 gem 的人都可以解释我做错了什么,那就太好了!我确定这与我的 def 在线方法设置不正确有关,但我不确定我将如何编写它。
users_controller.rb:
def online
@users = User.online
end
online.html.erb:
<table>
<thead>
<tr>
<th>Users Online</th>
</tr>
</thead>
<tbody>
<% @users.each do |user| %>
<tr>
<td><%= user.email %></td>
<td><%= user.user_type %></td>
<% if user_signed_in? && current_user.user_type == 'admin' %>
<td><%= link_to 'Destroy', user, method: :delete, data: { confirm: 'Are you sure you wish to delete this user?' } %></td>
<% end %>
</tr>
<% end %>
</tbody>
</table>
user.rb:
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :onliner
application_controller.rb:
before_filter { |c| current_user.track unless current_user.nil?}
您做的一切都是正确的,看起来 onliner
gem 中有错误。 User.online
returns nil
而不是您的空数组。我建议你在 gem 回购中打开一个问题。或者使用其他一些 gem.