如何在rails 3.2.3中使用分页?

时间:2012-06-03 04:48:41

标签: ruby-on-rails will-paginate

我是RoR的新手,我想在我的应用程序中加入分页。

执行此任务的最佳方法是哪种?请建议我。是否可以在rails 3.2.3中使用will_paginate gem?

如果是,我应该在哪种方法中包括:

Post.where(:published => true).paginate(:page => params[:page]).order('id DESC')  

Post.page(params[:page]).order('created_at DESC')    

1 个答案:

答案 0 :(得分:3)

是的,可以在rails 3.2.3应用程序中使用will_paginate gem。

代码将取决于每个操作的执行情况。

以下代码通常采用#index方法,PostsController

@posts = Post.where(:published => true).paginate(:page => params[:page]).order('id DESC')

因此,在posts/index.html.erb视图文件中,您可以使用以下代码显示分页链接:

<%= will_paginate @posts %>