开发者

Kaminari: paginate a scoped model. Controller/View responsibility

开发者 https://www.devze.com 2023-03-01 14:59 出处:网络
I\'m not sure i\'ve entitled the question correctly. In my project I have a categories controller with show action

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.


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|
0

精彩评论

暂无评论...
验证码 换一张
取 消