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

me again ...

I need to show 10, 20 or 50 results, the number of results per page with selection options in my message list using the will_paginate plugin

Can you help me?

Thanks!

+4
source share
3 answers

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

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

(in 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]) 
+17
source

To set the default class

 class Post < ActiveRecord::Base def self.per_page 25 end end 

Or based on request by request, use per_page in your call

 class Post <ActiveRecord::Base def self.posts_by_paginate paginate(:all, :per_page => 25, :conditions => ["published = ?", true]) end end 
+1
source

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 
+1
source

All Articles