Rails - will_paginate says “variable appears to be empty. Did you forget to pass the collection object for will_paginate?”

后端 未结 1 608
死守一世寂寞
死守一世寂寞 2021-01-23 23:47

I have this query:

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

and in

相关标签:
1条回答
  • 2021-01-24 00:09

    You can do something like this in your controller:

    @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 }
    

    Then change the line with the pagination in your view:

    = will_paginate @car_paginator
    

    The problem is: When you use the paginate method the results is not a normal array or database relation but an object that has some extra information like @size. If you run group_by on that you remove that extra attributes and return a plain hash.

    0 讨论(0)
提交回复
热议问题