will_paginate gem : Limit number of pages
The following command:
<%= will_paginate @yourwhatevers, inner_window: 3, outer_window: 1 %>
accomplishes this. Let's say you are on page 15 of 36, you will get:
Previous 1 2 ... 12 13 14 15 16 17 18 .. 35 36 Next
inner_window
, in other words how many to the right and left from current page, defaults to 4, but for better, you could make it 1 or 2. outer_window defaults to 1, so my line above could not contain it at all
Limit number of pages in will_paginate
I found the best solution was to do this:
@results = Model.search(...)
if @results.total_pages >= (1000/Model.per_page)
class << @results; def total_pages; 1000/Model.per_page; end end
end
Where the 1000 is preferably not hardcoded :). This gets you the correct behaviour of the will_paginate view helper for free,
Will Paginate: Limit number of results
people = Person.limit(100).paginate(:per_page => 5)
Limiting will_paginate to show only 2 pages with 5 objects per page
Thanks to the question Limit number of pages in will_paginate all one has to do is pass the total_entries parameter into the paginate function. So my code looks like this:
@nominations = Hotel.where(winner: false).paginate(page: params[:page], per_page: 5, total_entries: 10)
How to limit the amount of pages displayed in the links of will_paginate
The inner_window
and outer_window
properties can be used to manipulate how many page links you see on each page. You can pass them to the will_paginate
method on your view (if you only want to limit the number of links for one page).
If you want to limit the number of pages shown throughout your application, the best approach is an initializer file (call it something like will_paginate.rb). You can set global configuration options like so:
WillPaginate::ViewHelpers.pagination_options[:inner_window] = 3
WillPaginate::ViewHelpers.pagination_options[:outer_window] = 2
Regarding will_paginate using an explicit per page limit - Rails 4.2.0
By using following code you can achieve this, but you need to decide that the limit for example, after page 4 all pages should contain per_page 30.
dynamic_paging = {"1" => 10, "2"=> 15, "3"=> 20, "4"=>25 }
per_page_after_four = 30
params[:page] = params[:page] || 1
if params[:page] < 5
@posts = Post.paginate(:page => params[:page], :per_page => dynamic_paging[params[:page]])
else
@posts = Post.paginate(:page => params[:page], :per_page => per_page_after_four)
end
update
And if you want to set the per_page from your view than here is the updated solution.
use select on the view.
<%= select_tag :per_page, options_for_select([10,15,20,25], params[:per_page].to_i),
:onchange => "if(this.value){window.location='?per_page='+this.value;}" %>
and in the controller.
@per_page = params[:per_page] || Post.per_page || 20
@posts = Post.paginate( :per_page => @per_page, :page => params[:page])
Hope this will help!
rails 4 will_paginate set count of links on pages
You must change value for inner_window
. Reference here.
So here is code:
<%= will_paginate @events, :inner_window => 2, :outer_window => 1 %>
Related Topics
Xlsx Compressed by Rubyzip Not Readable by Excel
How to Call a Parent App's Helper Method from a Rails 3.1 Engine
How to Stub a Method of an Included Module with Rspec
Is Ruby's Stdlib Logger Class Thread-Safe
Why Is Ruby String.Hash Inconsistent Across MAChines
Problems Keeping an Object in an Array, Ruby Issues and Rails Issues
Problem with Rspec Test, Undefined Method 'Post'
Twitter 3-Legged Authorization in Ruby
Ruby Elegant Way to Return Min/Max If Value Outside Range
Multi Level Block Method Is Generating Issue
Creating Spectral Heat Maps or Intensity Maps from Cdip Data Using Ruby
How Get Best Performance Rails Requests Parallel Sidekiq Worker
Generate Array of Numbers That Fit to a Probability Distribution in Ruby
Convert Array-Of-Hashes to a Hash-Of-Hashes, Indexed by an Attribute of the Hashes
How to Scrape Pages Which Have Lazy Loading
Colorized Output Breaks Linewrapping with Readline