This is my pagination, I want to limit the number of pages to 10, for example, to don't have the same problem as in the picture.
How can I do it using will_paginate
gem?
will_paginate provides a simple API for performing paginated queries with Active Record, DataMapper and Sequel, and includes helpers for rendering pagination links in Rails, Sinatra and Merb web apps.
On web pages, sometimes you have many rows of information, lets say: a list of users on the system. In that situation having all the users listed on the same page is not very usable. For that we use pagination that allow us to divide the rows on several pages and order them based on date, name or other field.
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
There is quite a few ways to do it depending on that you want to do. Check out this answer:
Limit number of pages in will_paginate
One example with limiting it to 100 entries with 10 on a page (hence 10 pages)
@posts = Post.paginate(:page => params[:page], :per_page => 10,
:total_entries => 100)
You could limit the amount of results to be 10 times your page size:
MyModel.limit(300).paginate(page: params[:page])
That would mean you couldn't return any more than 300 results and therefore no more than 10 pages worth.
Alternatively you could explore some of the will_paginate
rendering options. You can configure the amount of pages display at the end of the pagination and around the current page - or even remove the page numbers altogether. For example:
will_paginate @results, inner_window: 1, outer_window: 1
will show Previous | 1 | ... | 9 | ... | 16 | Next
for the example in the question.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With