1

I'm using kaminari gem for pagination in my rails application to paginate the categories. I have implemented all the process, and the pagination links are also being shown. But this is not effecting the table elements list.

Index action in controller

    def index
            @categories = current_user.categories
            @categories =  Kaminari.paginate_array(@categories).page(params[:page]).per(5)
    end

Index view

<h1>Categories</h1>
    <div class="well">
    <%= link_to "+ New Category", new_category_path, class: "btn btn-success" %>
    </div>

    <%= paginate @categories %>
    </br>
    <%= page_entries_info @categories %>
    <table class="table table-bordered table-striped">
      <thead>
        <tr>
          <th>Name</th>
          <th>Actions</th>
        </tr>
      </thead>
      <tbody>
        <% if current_user.categories.count == 0 %>
          <tr>
            <td colspan="2">
              No categories found.  Click the button above to add a new one.
            </td>
          </tr>
        <% else %>
          <% current_user.categories.each do |category| %>
            <tr>
              <td><%= link_to category.name, category %></td>
              <td><%= link_to "Edit", edit_category_path(category), class: "btn btn-primary" %>
                  <%= link_to "Delete", category, method: :delete,
                        data: { confirm: "You sure?" }, class: 'btn btn-small btn-danger' %>
              </td>
            </tr>
          <% end %>
        <% end %>
      </tbody>
    </table>

Screenshotenter image description here

As shown in the above picture it is showing all the 7 entries for a pagination of 5/page.

Can some one please explain the reason why it is not working.

Atchyut Nagabhairava
  • 1,295
  • 3
  • 16
  • 23

1 Answers1

2

I think issue is with your array type. Please try following code.

  def index  
    @categories = current_user.categories.all
    unless @categories.kind_of?(Array)
      @categories = @categories.page(params[:page]).per(5)
    else
      @categories = Kaminari.paginate_array(@categories).page(params[:page]).per(5)
    end
  end

Have you tried this way? Its more optimised, only fetch those records that required.

@categories = current_user.categories.page(params[:page]).per(5)

Solution adopted from: rails 3,Kaminari pagination for an simple Array

Community
  • 1
  • 1
Aamir
  • 16,329
  • 10
  • 59
  • 65