如何在 rails 3.2.3 中使用分页? [英] How to use pagination in rails 3.2.3?

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

问题描述

我是 RoR 的新手,我想在我的应用程序中包含分页.

I'm new in RoR, and I want to include pagination in my application.

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

Which is the best way to perform this task? Please suggest me. Is it possible to use will_paginate gem in rails 3.2.3 ?

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

If yes, in which method I should include:

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

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

推荐答案

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

Yes, it is possible to use will_paginate gem in rails 3.2.3 applications.

代码将取决于每个操作将执行的内容.

And the code will depends on what each action will perform.

下面的代码通常使用 #index 方法,在您的 PostsController 中.

The code below usually goes on a #index method, in your PostsController.

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

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

So, in your posts/index.html.erb view file you can use the following code to display the pagination links:

<%= will_paginate @posts %>

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

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