will_paginate和.sort => [英] will_paginate and .sort =>

查看:116
本文介绍了will_paginate和.sort =>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近安装了will_paginate gem,它运行良好,但是我希望它可以与当前的设置一起使用.现在,我在同一页面上显示所有提要(或帖子),但它们是按点击该页面的人数排序的:

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. 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")

所以我认为1 + 1 = 2并尝试通过以下操作将两者结合:

So I thought 1+1=2 and tried to combine the two by doing:

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:

#的未定义方法"total_pages"

undefined method `total_pages' for #

我希望它能正常工作,这会非常好:).但是,如果它不起作用,还有其他方法可以这样做吗?

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?

提前谢谢!

推荐答案

这是因为will_paginate默认仅在ActiveRecord关系上有效.使用排序后,它将转换为数组.如果要使will_paginate与数组一起使用,则需要在控制器中要求对它的支持.

It's because will_paginate works by default only on ActiveRecord relationships. 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.

require 'will_paginate/array' 

这篇关于will_paginate和.sort =&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆