Kaminari::Cells paginate method not rendering anything

1.1k Views Asked by At

I'm using the Kaminari::Cells gem, and when I use the paginate method in a cell view, nothing shows up. I checked, and the paginate method is just returning "\n".

3

There are 3 best solutions below

2
On BEST ANSWER

I'm not sure why it works, but athlon-krum suggested removing the paginator.render do block of the _paginator.html.erb Kaminari view file, changing it from this:

<%= paginator.render do %>
  <%- pagination_class ||= '' %>
  <ul class="pagination <%= pagination_class %>">
    <%= 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? %>
  </ul>
<% end %>

to this:

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

and that seems to work. Don't forget to prepend paginator. to the Kaminari method calls to make it work (the examples above show this change, but it's easy to miss).

0
On

I know this question is nearly 6 years old, but it sure helped me get Kaminari and Cells playing nice together. The kaminari-cells gem doesn't work with Rails 6, so this is what I did to get it to work in my project. It's basically just two files, so I added them to my project.

# app/helpers/kaminary/helpers/cells_helper.rb
require 'kaminari/helpers/helper_methods'
require 'cell/partial'

module Kaminari
  module Helpers
    module CellsHelper
      include Kaminari::Helpers::HelperMethods
      include ActionView::Helpers::OutputSafetyHelper
      include ActionView::Helpers::TranslationHelper
      include Cell::ViewModel::Partial

      def paginate(scope, paginator_class: Kaminari::Helpers::Paginator, template: nil, **options)
        options = options.reverse_merge(:views_prefix => "../views/")
        super
      end
    end
  end
end
# app/models/concerns/kaminary/cells.rb
module Kaminari
  module Cells
    extend ActiveSupport::Concern

    included do
      include Kaminari::Helpers::CellsHelper
    end
  end
end
1
On

In my experience it was problem of that in kaminari #paginate helper assigns @template as self of the place where the helper is called. In usual Rails view @template will be an anonymous class, view template that inherits from ActionView::Base. In cell @template will be instance of cell itself. kaminari when render uses ActionView::OutputBuffer. That makes the difference because the view #render and the cell #render behaves differently and the cell #render do not put anything into output buffer.

Quick fix is to omit output buffer:

Kaminari::Helpers::Paginator.class_eval do
  def render(&block)
    instance_eval(&block) if @options[:total_pages] > 1
    # @output_buffer
  end
end