如何用康康做分页? [英] How to do pagination with cancan?

查看:94
本文介绍了如何用康康做分页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用cancan进行分页,但是如何将其与will_paginate之类的宝石集成尚不明确.

I'm looking to do pagination with cancan however it's not obvious how to integrate this with gems such as will_paginate.

理想情况下,cancan的load_resource将委托给will_paginate并添加其他条件.例如,在cancan中,我已经声明了来宾用户

Ideally cancan's load_resource will delegate to will_paginate and add extra conditions. For example in cancan I've declared guest users

can :read, Post, :published => true

,这由load_resource自动处理.但是,我想在所有这些结果中使用will_paginate页面.

and this is handled automatically by load_resource. However I'd then like to have will_paginate page through all these results.

任何想法.

致谢

布拉德

推荐答案

使用kaminari很容易做到 https://github.com/amatsuda/kaminari

This is simple to do with kaminari https://github.com/amatsuda/kaminari

在我的PostsController中,我只是做

in my PostsController I just do

  before_filter :load_by_pagination, :only => :index
  def load_by_pagination
    @posts = Post.accessible_by(current_ability).order("published_date desc").page params[:page]
  end 

请注意,kaminari可与新的rails3 ActiveRelation框架正常工作,因此 可以将方法链接在一起以建立作用域.就像CanCan一样 两者都是ActiveRelation友好的,它们只是链在一起.

Note that kaminari works properly with the new rails3 ActiveRelation framework so it's possible to chain methods together to build up scope. And as CanCan is also ActiveRelation friendly they both just chain together.

这篇关于如何用康康做分页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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