简体   繁体   English

will_paginate和.sort =>

[英]will_paginate and .sort =>

I recently got the will_paginate gem installed and it works great, but I would like it to work with the setup I currently have. 我最近安装了will_paginate gem,它运行良好,但是我希望它可以与当前的设置一起使用。 Right now I show all feeds (or posts) on the same page, but they are sorted after how many people clicked on that page: 现在,我在同一页面上显示所有提要(或帖子),但它们是按点击该页面的人数排序的:

controller.rb controller.rb

 @t = Time.now
 @h1 = @t - 1.hour
 @feeds = Feed.find(:all, :order => "created_at DESC").sort { |p1, p2| p2.impressionist_count(:filter=>:all, :start_date=>@h1) <=> p1.impressionist_count(:filter=>:all, :start_date=>@h1)}

Now... I tested the paginate gem and it works fine if I do this in the controller: 现在...我测试了分页宝石,如果我在控制器中执行此操作,它将正常工作:

controller.rb controller.rb

 @feeds = Feed.paginate(:per_page => 10, :page => params[:page], :order => "created_at DESC")

So I thought 1+1=2 and tried to combine the two by doing: 因此,我认为1 + 1 = 2并尝试通过执行以下操作将两者结合:

controller.rb controller.rb

@feeds = Feed.paginate(:per_page => 10, :page=>params[:page], :order => "created_at DESC").sort { |p1, p2| p2.impressionist_count(:filter=>:all, :start_date=>@h1) <=> p1.impressionist_count(:filter=>:all, :start_date=>@h1)}

I'm not able to sort my posts and paginate them. 我无法对帖子进行排序和分页。 I get an error when I try to load the page: 尝试加载页面时出现错误:

undefined method `total_pages' for # #的未定义方法“ total_pages”

I would like this to work, it would be pretty sweet :). 我想这个工作,这将非常甜:)。 However, if it does not work, is there any other way of doing this? 但是,如果它不起作用,还有其他方法可以这样做吗?

Thanks in advance! 提前致谢!

It's because will_paginate works by default only on ActiveRecord relationships. 这是因为will_paginate默认仅在ActiveRecord关系上起作用。 Once you use the sort, it's converted to an array. 使用排序后,它将转换为数组。 If you want will_paginate to work with an array, you'll need to require support for it in your controller. 如果要使will_paginate与数组一起使用,则需要在控制器中需要对其的支持。

require 'will_paginate/array' 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM