nil:NilClass、paginate_with_kaminari 的未定义方法“页面”

undefined method `page' for nil:NilClass, paginate_with_kaminari

我正在使用 paginate_with_kaminari gem 在我的 rails 应用程序中实现分页。有时我会收到此错误 "undefined method `page' for nil:NilClass".

@notifications = paginate current_user.push_records.where(:n_status.in => [PushRecord::N_STATUS[:NEW_REGISTERED],PushRecord::N_STATUS[:UNINSTALLED]]).to_a , per_page: 20

在上面的代码中,params[:page] 将出现,其中包含必须获取的页码。

如果未获取任何结果,则它将 return nil 。在这种情况下,@notifications 将为空。但是在 "current_user.push_records.where(:n_status.in => [PushRecord::N_STATUS[:NEW_REGISTERED],PushRecord::N_STATUS[:UNINSTALLED]])" 中,如果你使用一些其他的数组方法,比如 sort 或其他一些函数,那么在这种情况下, sort 不能为 nil class 完成并且会抛出错误 .