通过哈希进行分页

发布于 2024-11-05 15:37:05 字数 1021 浏览 5 评论 0原文

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

的......你能帮我吗?

{#<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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月依秋水 2024-11-12 15:37:05

第一的。当您拥有许多农场时,您的代码运行速度会非常慢。

第二。如此简单的分页:

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,我很懒:)

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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文