本文介绍了Rails:如何使用 will_paginate 每页显示 10、20 或 50 个结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

又是我...

我需要使用 will_paginate 插件在我的帖子列表中显示 10 或 20 或 50 个结果每页的结果数量

I need show 10 or 20 or 50 results number of results per page with a select options in my list of posts using will_paginate plugin

你能帮我吗?

谢谢!

推荐答案

貌似 OP 也在这里问过:http://railsforum.com/viewtopic.php?id=33793 并得到了更好的答案.

Looks like the OP also asked here: http://railsforum.com/viewtopic.php?id=33793 and got much better answers.

为了适应那里的最佳解决方案,这是我喜欢的:

To adapt the best solution there, here's what I like:

(在视图中)

<%= select_tag :per_page, options_for_select([10,20,50], params[:per_page].to_i),
       :onchange => "if(this.value){window.location='?per_page='+this.value;}" %>

(在控制器中)

@per_page = params[:per_page] || Post.per_page || 20
@posts = Post.paginate( :per_page => @per_page, :page => params[:page])

这篇关于Rails:如何使用 will_paginate 每页显示 10、20 或 50 个结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 01:44