If you have a web page with a list of data more than a screen height, it won't be look good. The user have to scroll down to see the data, and mostly it will affect the performance of your website. If there are 100 rows of data in a list, we will fetch all the data to show in the list. But if we are using pagination we can limit the number of data to fetch, for example if we need to show only 10 rows of data per page we only need to fetch 10 records, not the entire data.

There is a gem called will_paginate which do all we need to do for a pagination in our rails web app. All you need to do is install the gem:

gem install will_paginate

Then when you are accessing model for data, use the paginate along with that:

@data = Data.paginate :page => params[:page]

Here params[:page] is the page number we need to fetch. This will come from the view because we will add some helper method in the view to show the page links:

<%= will_paginate @data %>

This code you can put it in the end of the list, so that when you click on the link it will send request to the same action with parameter as next page number.

blog comments powered by Disqus
Heroku →
← through association