{#<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}
我剪切了样本(也可以在那里查看http://pastie.org/1871728)。我需要为这个哈希分页。使用kaminari或will_paginate没什么区别。 如果这个问题我在视图中输出这个哈希就像
- @sort_farms.count.times do
- farm = @sort_farms.shift
%p
= image_tag farm[0].logo.url.to_s
= farm[0].harvest
等等......拜托,请帮帮我。
答案 0 :(得分:1)
首先。当您拥有许多农场时,您的代码将会非常缓慢。
二。如此简单的分页:
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]
所以在控制器中我们得到了一堆农场。 您应该将所有这些分页工作包装到您的模型中
现在,观点:
@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
就是这样。
将此视图转换为HAML或erb,我很懒:)