How to do pagination with cancan?

萝らか妹 提交于 2019-12-05 07:13:52

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

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 

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.

You can also do something like this:

class PostsController 

  def index
    @posts = @posts.page(params[:page])     
  ...
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!