{#<Farm id: 1, name: "Farm A", harvest: 2, offers: "Buy now, Delivery", about: "We rocks!",
created_at: "2011-04-23 12:33:43", updated_at: "2011-04-25 09:02:15",logo: "images__9_.jpg", latitude: 42.3214, longitude: -71.0722, address: "555 Dudley Street, Boston, MA", votes_count: 2>=>4482.753159399051, #<Farm id: 2, name: "Farm B",harvest: 3,
offers: "Buy Now, Delivery", about: "We rock the farm A!", created_at: "2011-04-23 12:36:29", updated_at: "2011-04-25 09:02:15", logo: "images__7_.jpg", latitude: 42.3442, longitude: -71.0995, address: "1345 Boylston Street,Boston, MA", votes_count: 2 > => 4482.571841402453}
I cut the sample (also can be viewed there http://pastie.org/1871728). I need a pagination for this hash. There is no difference what using kaminari or will_paginate. If this matter im output in the view this hash like
- @sort_farms.count.times do
- farm = @sort_farms.shift
%p
= image_tag farm[0].logo.url.to_s
= farm[0].harvest
and so on... Can you help me, please.
First. Your code will work very slow when you'll have many of farms.
Second. So simple pagination:
per_page = 50.0 # show 50 farms per page
@page = params[:page] ? params[:page].to_i : 1
@address = get_user_address
@farms = Farm.all
@pages = (Farm.count/per_page).ceil
@sorted_farms = @farms.sort_by{|farm| farm.distance_from(@address)}[(@page-1)*per_page, per_page]
So in controller we get a bunch of farms. You should wrap all this pagination work into your model
Now, views:
@sorted_farms.each do |farm|
image_tag farm.logo.url
farm.harvest
distance: farm.distance_from(@address)
end
Pages:
1.upto(@pages).do |page|
link_to page, :page => page
end
that's it.
Convert this view into HAML or erb, I was lazy :)