Rails:不支持:TrueClass错误

时间:2016-06-08 19:23:49

标签: ruby-on-rails

当我尝试查看unsupported: TrueClass页面时,我收到的错误是users#show之前从未见过的。我有一个基本的应用程序listsitems。我有这个代码,它工作得很好:

    <% if @lists.count != 1 %>
      <li><%= @lists.count %> Personal Lists</li>
    <% else %>
      <li><%= @lists.count %> Personal List</li>
    <% end %>

    <% if @shared_lists.count != 1 %>
      <li><%= @shared_lists.count %> Shared Lists</li>
    <% else %>
      <li><%= @shared_lists.count %> Shared List</li>
    <% end %>

    <% if @items_delegated_to.count != 1 %>
      <li><%= @items_delegated_to.count %> Tasks Delegated to Me</li>
    <% else %>
      <li><%= @items_delegated_to.count %> Task Delegated to Me</li>
    <% end %>

但是当我添加此块时出现错误:

    <% if @items_delegated_by.count != 1 %>  ***ERROR CALLED ON THIS LINE***
      <li><%= @items_delegated_by.count %> Tasks Delegated to Others</li>
    <% else %>
      <li><%= @items_delegated_by.count %> Task Delegated to Others</li>
    <% end %>

这是我的users_controller

class UsersController < ApplicationController
  def show
    @user = User.find(params[:id])
    @lists = current_user.lists
    @items = current_user.items
    @shared_lists = @lists.where(:shared_with == current_user.id)
    @items_delegated_by = @items.where(:delegated_to != "")
    @items_delegated_to = @items.where(:delegated_to == current_user.email)
  end
end

这是神秘的,因为它与它之前的代码完全不同,它没有问题。任何人都可以解释一下不受支持的TrueClass是什么或为什么我会收到此错误?

1 个答案:

答案 0 :(得分:1)

尝试将您的查询更改为:

 @items_delegated_by = @items.where.not(delegated_to: "")