Search code examples
ruby-on-railspaginationkaminari

Kaminari: paginate a scoped model. Controller/View responsibility


I'm not sure i've entitled the question correctly. In my project I have a categories controller with show action

def show
    @category = Category.find params[:id]
end

And in my view I render all the posts associated with this category

@category.posts.each do |post|
    link_to post.title, post

So now I want to add pagination with kaminari. I believe I could just change @category.posts.each to @category.posts.page(params[:page]).each, but I also think that this should be responsibility of the controller. Or am I wrong? Maybe it's totally fine?

Thanks everyone.


Solution

  • Your show method should look like this:

    def show
      @posts = Category.find(params[:id]).posts.page(params[:page])
    end
    

    And in view:

    @posts.each do |post|