Search code examples
ruby-on-railskaminari

How to force kaminari show the first link given the page is on the first page


How to force kaminari show the first link given the page is on the first page

My expectation is that even the page is alreday on the first page.

The pagination should also show the previous_link, the first_page_link.But only display the link text, disable the link function


Solution

  • If you want to show the links "first_page" and "previous_page" (or the "last_page" and "next_page") always even if is the first page, you should run this command in your terminal, you must be located at the same directory as the project

    rails g kaminari:views default
    

    This will generate the files for the kaminari views, then you should go to the "_paginator.html.erb" and remove the "unless"

    views/kaminari/_paginator.html.erb

    <%= paginator.render do %>
      <nav class="pagination">
        <%= first_page_tag unless current_page.first? %>
        <%= prev_page_tag unless current_page.first? %>
        <% each_page do |page| %>
          <% if page.left_outer? || page.right_outer? || page.inside_window? %>
            <%= page_tag page %>
          <% elsif !page.was_truncated? %>
            <%= gap_tag %>
          <% end %>
        <% end %>
        <%= next_page_tag unless current_page.last? %>
        <%= last_page_tag unless current_page.last? %>
      </nav>
    <% end %>
    

    So you will need to change that file and leave it like this

    views/kaminari/_paginator.html.erb

    <%= paginator.render do %>
      <nav class="pagination">
        <%= first_page_tag %>
        <%= prev_page_tag %>
        <% each_page do |page| %>
          <% if page.left_outer? || page.right_outer? || page.inside_window? %>
            <%= page_tag page %>
          <% elsif !page.was_truncated? %>
            <%= gap_tag %>
          <% end %>
        <% end %>
        <%= next_page_tag %>
        <%= last_page_tag %>
      </nav>
    <% end %>
    

    Hope it helps :D