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

I show all used_cars_hash in my view. But when I add <% will_paginate @user_cars_hash %> it gives an error

How to show pagination counter on array?

Because when we add <% will_paginate @user_cars_hash %>

it gives an error

undefined method total_pages' for #<Array:0x007f1edef43c48>

Haseeb Ahmad
  • 7,914
  • 12
  • 55
  • 133

1 Answers1

0

will_paginate does not work with the plain Ruby array you get by calling used_cars.as_json. Try passing used_cars to it:

# 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 %>

If you absolutely have to use the array for pagination, you can do this:

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 %>
Mate Solymosi
  • 5,699
  • 23
  • 30