嗨,我正在尝试使用 total_count 方法来计算我所有的产品并显示数量
@products.total_count
这工作得很好,它给了我产品的总数,甚至可以使用搜索结果但是当我点击一个产品时它说
nil:NilClass 的未定义方法“total_count”
嗨,我正在尝试使用 total_count 方法来计算我所有的产品并显示数量
@products.total_count
这工作得很好,它给了我产品的总数,甚至可以使用搜索结果但是当我点击一个产品时它说
nil:NilClass 的未定义方法“total_count”
但是,当我单击产品时,它会显示
undefined method
total_count' 为 nil:NilClass`
您将查看products#show
并通常设置一个@product
实例变量。正如@99miles 所说,@products
此时不存在。
出于性能原因,我建议使用计数器缓存列(在产品上) 。