31Mar/102
How to paginate outside of the database with will_paginate
will_paginate is a great plugin. I have been using it since I started on Rails.
If you want to paginate records that are inside your database it works great.
But what if you want to paginate records that come from, say an external webservice?
Well, you can use the WillPaginate::Collection class.
This class will let you mimic the collection that will work on a pager, so you can use the view helper pager seamlessly throughout your app.
Just use it like this:
@entries = WillPaginate::Collection.create(1, 10) do |pager|
result = Webservice.get(...) # get your results
# inject the result array into the paginated collection:
pager.replace(result)
unless pager.total_entries
# the pager didn't manage to guess the total count, do it manually
pager.total_entries = result.count
end
end
Agosto 8th, 2010 - 15:12
thank you
Agosto 26th, 2010 - 03:36
Thanks for the advice. Will put it to work. Tom