Sunday, 15 June 2014

Pagination In Rails4 Using 'will_paginate' Gem

Pagination In Rails4 Using 'will_paginate' Gem:


1, add below line to Gemfile
gem 'will_paginate'
2, Run bundler
$bundle install
3, Add below line to your method in controller.
#to define per_page value
@products=@category.products.all.paginate(:page => params[:page], :per_page => 10)
4, Add below code to related html.erb file.
<%= will_paginate @products %>
#It will display only simple links, Inorder to apply different styles to pagination download pagination.css and pagination.sass from http://mislav.uniqpath.com/will_paginate/
5, keep that two files in app/assets folder.
6, Add below two lines to application.html.erb (i.e, your layout).
<%= stylesheet_link_tag :pagination %>
<%= stylesheet_link_tag “pagination.sass” %>
7, Add related css class to your pagination tag in related view file.
<div class="digg_pagination" align="right"><%= will_paginate @products %></div>
Here we have three different types of pagination classes they are specified in paginate.css files:
  • digg_pagination
  • apple_pagination
  • flickr_pagination
we can use thouse classes according to our requirement.

Happy Rails Coding................

No comments:

Post a Comment