Rails: Showing 10 or 20 or 50 results per page with will_paginate how to?

ⅰ亾dé卋堺 提交于 2019-12-03 13:36:43

问题


me again...

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

Can you help me please?

Thanks!


回答1:


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:

(in the view)

<%= 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;}" %>

(in the controller)

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



回答2:


To set a class wide Default

class Post < ActiveRecord::Base

  def self.per_page
    25
  end

end

Or on a query by query basis use the per_page in your call

class Post <ActiveRecord::Base

  def self.posts_by_paginate
    paginate(:all, :per_page => 25, :conditions => ["published = ?", true])
  end

end



回答3:


Here is what I will do

Class UsersController < ApplicationController
    def index
        @users = User.paginate(:all, :page => params[:page], :per_page => params[:number_of_records])
    end
end


来源:https://stackoverflow.com/questions/1281093/rails-showing-10-or-20-or-50-results-per-page-with-will-paginate-how-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!