will_paginate Is Perfect Gem for Your Advanced Ruby on Rails Application

If You guys looking any Pagination gem for your ruby application or advanced blog site, then I'll prefer
you Use 'Will Paginate'. Its easy and work perfect on Rails 4 too. You can Install the gem, Here I'll explain you how you can use this gem on your blog. Lets see

1. First step:
$ gem 'Will Paginate'
in your gemfile, next

2. now Add the code in your controller file, In my case I'm using @product model, So In my case, I'll add this code in Product Controller,

def index
  @products = Product.order("name").page(params[:page]).per_page(5)
end

3. Third step,

<%= will_paginate @products %>

or

<%= will_paginate @products, previous_label: h("<"), next_label: h(">") %>

in your Product Index view. or if you want to customise it with your conditions so no need to add will_paginate, Just add this code

<!-- or custom pagination -->
<% if @products.previous_page %>
  <%= link_to "< Previous", params.merge(page: @products.previous_page) %>
<% end %>
Page <%= @products.current_page %> of <%= @products.total_pages %>
<% if @products.next_page %>
  <%= link_to "Next >", params.merge(page: @products.next_page) %>
<% end %>

4. Thats it, now you can check this pagination on your blog, I'm sure it will be worked perfectly.  now if you want to make it interesting and beautiful, Just add the css in your stylesheet...

.pagination {
  background: white;
  cursor: default;
  height: 22px;
  a, span, em {
    padding: 0.2em 0.5em;
    display: block;
    float: left;
    margin-right: 1px;
  }
  .disabled {
    display: none;
  }
  .current {
    font-style: normal;
    font-weight: bold;
    background: #2e6ab1;
    color: white;
    border: 1px solid #2e6ab1;
  }
  a {
    text-decoration: none;
    color: #105cb6;
    border: 1px solid #9aafe5;
    &:hover, &:focus {
      color: #000033;
      border-color: #000033;
    }
  }
  .page_info {
    background: #2e6ab1;
    color: white;
    padding: 0.4em 0.6em;
    width: 22em;
    margin-bottom: 0.3em;
    text-align: center;
    b {
      color: #000033;
      background: #2e6ab1 + 60;
      padding: 0.1em 0.25em;
    }
  }
}

Enjoy will_paginate gem, You can use it with lots of another helps at will_paginate gem at github.


Tags: Ruby on Rails Tutorials, Ruby on Rails Videos, Ruby Gems, Gems Use trick, Ruby code, Rails servers

Comments