如何在视图中显示用于分页的数组的分页计数器

时间:2017-07-08 22:40:17

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

used_cars = UsedCar.paginate(:page => params[:page], :per_page => 5)
used_cars_hash = used_cars.as_json

我在视图中显示所有used_cars_hash。但是当我添加<% will_paginate @user_cars_hash %>时 它给出了一个错误

如何在阵列上显示分页计数器?

因为我们添加<% will_paginate @user_cars_hash %>

它出错了

未定义的方法total_pages' for #<Array:0x007f1edef43c48>

1 个答案:

答案 0 :(得分:0)

will_paginate不适用于通过调用used_cars.as_json得到的普通Ruby数组。尝试将used_cars传递给它:

# no need to turn it into a plain array
@used_cars = UsedCar.paginate(:page => params[:page], :per_page => 5)

# in the view:
<%= will_paginate @used_cars %>

如果你绝对必须使用数组进行分页,你可以这样做:

require 'will_paginate/array'

# you can call `paginate` on plain arrays
used_cars = UsedCar.all
@used_cars_array = used_cars.as_json.paginate(:page => params[:page], :per_page => 5)

# in the view:
<%= will_paginate @used_cars_array %>