Rails - will_paginate说"变量似乎是空的。您是否忘记为will_paginate传递集合对象?"

时间:2015-01-26 00:03:39

标签: ruby-on-rails ruby will-paginate

我有这个问题:

@cars = Car.paginate(page: params[:page], per_page: 5).order(created_at: :desc).group_by { |r| r.created_at.to_date }

并在视图中:

= will_paginate @cars

但我收到此错误(在site#index中呈现):

The @site variable appears to be empty. Did you forget to pass the collection object for will_paginate?

我还试图以这种方式更新查询:

@cars = Car.order(created_at: :desc).paginate(page: params[:page], per_page: 5).group_by { |r| r.created_at.to_date }

但错误仍然相同。

如何解决此问题?

谢谢

1 个答案:

答案 0 :(得分:1)

您可以在控制器中执行以下操作:

@car_paginator = Car.paginate(page: params[:page], per_page: 5).order(created_at: :desc)
@cars = @car_paginator.group_by { |r| r.created_at.to_date }

然后在视图中使用分页更改行:

= will_paginate @car_paginator

问题是:当您使用paginate方法时,结果不是正常的数组或数据库关系,而是具有@size等额外信息的对象。如果您在其上运行group_by,则删除该额外属性并返回纯哈希。