2016-06-08 79 views
1

我收到一個錯誤,當我嘗試查看我的users#show頁面時,我從未見過unsupported: TrueClass。我有一個基本的應用程序listsitems。我有這個代碼和它工作得很好:Rails:不支持:TrueClass錯誤

<% 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

試着改變你的查詢到這一點:

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

第一個工作就像一個魅力,但第二個實際上是由它產生'0',當它以前讀'2'(修正值)。 – Liz

+0

我的壞,編輯它 –

+0

仍然會出現'0' .... – Liz