kaminari gem

Kaminari Gem

What is Kaminari?

Kaminari is a pagination gem for Ruby on Rails that provides a simple and flexible way to paginate records in your application. With Kaminari, you can easily add pagination to your views and controllers, and customize the pagination options to fit your specific needs.

Installing Kaminari

To use Kaminari in your Rails application, you first need to install the gem. You can do this by adding the following line to your Gemfile:

gem 'kaminari'

After adding the gem to your Gemfile, run bundle install to install the gem and its dependencies.

Using Kaminari in Your Controllers

Once you’ve installed Kaminari, you can start using it in your controllers to paginate records. Here’s an example of how to paginate a list of users in a UsersController:

class UsersController < ApplicationController
def index
@users = User.order(:name).page(params[:page]).per(10)
end
end

In this example, we’re using the page method to specify the current page number, and the per method to specify the number of records per page. The order method is used to sort the users by their name.

Using Kaminari in Your Views

Once you’ve paginated your records in your controller, you can use Kaminari’s view helpers to display the pagination links in your views. Here’s an example of how to display the pagination links in an index.html.erb view:

<%= paginate @users %>

In this example, we’re using the paginate helper to generate the pagination links for the @users collection. Kaminari will automatically generate links for the previous, next, and numbered pages based on the current page and the number of pages in the collection.

Customizing Pagination Options

Kaminari provides a variety of customization options that you can use to configure the pagination links and behavior. Here are some examples of common customization options:

  • Changing the Default Number of Records Per Page

By default, Kaminari displays 25 records per page. You can change this by setting the default_per_page configuration option in an initializer file:

#config/initializers/kaminari.rb
Kaminari.configure do |config|
config.default_per_page = 10
end

In this example, we’re setting the default number of records per page to 10.

  • Changing the Labels of the Pagination Links

You can customize the labels of the pagination links by using the param_name and params options in the paginate helper:

<%= paginate @users, param_name: :page_number, params: { foo: 'bar' } %>

In this example, we’re setting the pagination parameter to page_number instead of the default page, and passing a foo=bar parameter to the generated pagination links. You can also customize the labels of the individual pagination links by using the theme option and creating a custom view template.

  • Customizing the Pagination Theme

Kaminari provides several built-in themes for the pagination links, including default, twitter-bootstrap, and materialize. You can customize the theme by setting the theme configuration option in an initializer file:

 
#config/initializers/kaminari.rb
Kaminari.configure do |config|
config.default_theme = :twitter-bootstrap
end
``
Related Posts

Leave a Comment

Your email address will not be published. Required fields are marked *

en_USEnglish