skip to content

With Kaminari in a Rails app, how do you customise the pagination markup with rails g kaminari:views, and how does paginate select a theme?

level: middleimportance: nice to knowfreq 18%

answer

  1. seven partials under app/views/kaminari
  2. default copies, a THEME name downloads
  3. --views-prefix for a namespace
  4. paginate @listings, theme: 'name'
  5. window, outer_window, left, right

basics

~10 s

bin/rails generate kaminari:views default copies the gem's paginator partials into app/views/kaminari for editing; another THEME name downloads that theme from the kaminari_themes GitHub repository. paginate @listings, theme: 'job_board' renders app/views/kaminari/job_board/ instead.

solid answer

~40 s

Kaminari renders `paginate` through seven partials: `_paginator`, `_page`, `_gap`, `_first_page`, `_prev_page`, `_next_page` and `_last_page`. Until you override them, the copies inside the gem are used. `bin/rails generate kaminari:views default` copies the ERB versions into `app/views/kaminari/`, where you change classes and markup, for example to match a CSS framework. `--views-prefix admin` writes them to `app/views/admin/kaminari/` instead. Any other THEME name makes the generator download that theme's templates from the `kaminari_themes` repository on GitHub, so it needs network access. For several looks in one app, put a set in `app/views/kaminari/job_board/` and call `paginate @listings, theme: "job_board"`. The `window:`, `outer_window:`, `left:` and `right:` options, or their `Kaminari.configure` defaults, decide which page numbers appear.

code

bash · 8 lines
bash
bin/rails generate kaminari:views default
# create  app/views/kaminari/_first_page.html.erb
# create  app/views/kaminari/_gap.html.erb
# create  app/views/kaminari/_last_page.html.erb
# create  app/views/kaminari/_next_page.html.erb
# create  app/views/kaminari/_page.html.erb
# create  app/views/kaminari/_paginator.html.erb
# create  app/views/kaminari/_prev_page.html.erb

go deeper

for a junior

Know that rails g kaminari:views default copies the pagination partials into app/views/kaminari so you can restyle them.

for a middle

Explain the seven partials, how theme: and views_prefix: change the partial path, and how window and outer_window choose the page numbers.

for a senior

Treat generated partials as app code: commit them, keep their accessibility attributes, and replace the rails-ujs remote: path with Turbo-era markup.

for a principal

Decide how much pagination markup to own in a design system, against taking a gem's defaults.

## Partials, not a builder Kaminari's `paginate` helper produces no HTML strings of its own. It renders **partials**, which makes the markup ordinary Rails view code: | partial | renders | |---|---| | `_paginator` | the container, `<nav class="pagination">`, and the loop over pages | | `_page` | one numbered link (no link for the current page) | | `_gap` | the `…` between windows | | `_first_page`, `_prev_page` | "« First" and "‹ Prev" | | `_next_page`, `_last_page` | "Next ›" and "Last »" | The labels come from I18n keys `views.pagination.first`, `previous`, `next`, `last` and `truncate`, so wording changes need only a locale file. ## The views generator ```bash bin/rails generate kaminari:views default bin/rails generate kaminari:views default --views-prefix admin bin/rails generate kaminari:views THEME ``` 1. **`default`** copies the gem's own ERB partials into `app/views/kaminari/`. Rails finds the app's copies before the gem's, so edits apply everywhere at once. 2. **`--views-prefix admin`** writes to `app/views/admin/kaminari/` instead, so an admin namespace can look different. 3. **Any other THEME name** downloads that theme's templates from the `amatsuda/kaminari_themes` repository through GitHub's API **at generation time**. With no name at all, the generator lists the available themes. Without network access, or with an unknown name, it only prints a message. 4. The generator's `-e haml` / `-e slim` options are deprecated, and their warning goes through `ActiveSupport::Deprecation.warn`, which fails on Rails 7.2 and later. Convert the ERB files yourself if the app uses another template engine. ## What each partial receives The partials are rendered with **locals**, listed in a comment at the top of each generated file: - every partial: `current_page` (a page object with `first?`, `last?` and `out_of_range?`), `total_pages`, `per_page` and `remote`; - `_paginator`: `paginator`, whose `each_page`, `page_tag`, `gap_tag` and `*_page_tag` methods drive the loop; - `_page`: `page` (with `current?`, `rel` and `number`) and `url`; - the edge partials: `url` for their target page. Any extra option passed to `paginate`, such as `paginate @listings, label: "Jobs"`, arrives in the partials as a local too, which is the supported way to feed a theme its own settings. ## Several themes in one app ```erb <%= paginate @listings, theme: "job_board" %> <%= paginate @applications, theme: "compact", window: 2 %> ``` `theme:` inserts a directory into the partial path: `kaminari/job_board/_paginator`. To create one, copy the default partials into `app/views/kaminari/job_board/` and edit them. `views_prefix:` does the same at the front of the path (`admin/kaminari/...`). ## Which page numbers appear The paginator decides which numbers to draw from four settings, each a `paginate` option or a `Kaminari.configure` default: - `window` (**4**): pages on each side of the current one; - `outer_window` (**0**): pages kept at both ends; - `left` / `right` (**0**): the same, per end; - anything else becomes a single `…` gap. With the defaults, page 10 of 40 shows 6-14, gaps, and the First/Prev/Next/Last text links. The template hides First/Prev on page 1 and Next/Last on the last page. ## Kaminari's partials in a Turbo-era app The bundled partials pass a `remote:` local into `link_to`. That produced a `data-remote="true"` attribute for rails-ujs, the JavaScript driver Rails used before 7.0. A new Rails 8.1 app loads Turbo instead, so `paginate @listings, remote: true` adds an attribute nothing reads. Paging inside part of the page is done with Turbo Frames around the list. That technique belongs to the Turbo topics; on the Kaminari side it only means editing the partials or passing `params:`. ## Practical advice - Generate the views **once**, commit them, and treat them as app code: they can lag behind later gem changes. - Keep accessibility attributes in the `nav` (the default sets `role="navigation"` and `aria-label="pager"`) when restyling. - Give a theme a meaningful name (`admin`, `compact`), not a CSS framework version you may later swap out.

  • Why might bin/rails generate kaminari:views bootstrap4 fail on a CI machine while kaminari:views default works?
    Only `default` is bundled with the gem. Any other name makes the generator call GitHub's API to list and download templates from the `kaminari_themes` repository, so without network access it lists no themes and prints a message instead of creating files. Generate locally and commit the partials.
  • How do you show fewer page numbers on a phone-sized listing page with Kaminari?
    Pass a smaller window to that call, `paginate @listings, window: 1`, or set `config.window` in `Kaminari.configure` for every call. Combine it with a theme whose `_paginator` drops the First/Last links if space is tight.

  • Thinks kaminari:views writes config/initializers/kaminari_config.rb; that is kaminari:config.
  • Expects paginate @listings, remote: true to page through Turbo in a new Rails 8.1 app.
  • Uses views_prefix: when the partials sit in app/views/kaminari/<name>/, which needs theme:.
  • Believes every theme ships inside the gem and works offline.
  • Edits the gem's installed partials instead of generating app copies.

Assumes Kaminari 1.2.2 on Rails 8.1, whose new apps load Turbo rather than rails-ujs, so the partials' remote: option yields a data-remote attribute that nothing handles.