4

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".

neurodynamic
  • 4,294
  • 3
  • 28
  • 39

3 Answers3

7

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).

neurodynamic
  • 4,294
  • 3
  • 28
  • 39
  • 1
    The above examples differ from the vanilla Kaminari partial obtained with `rails g kaminari:views default -e erb` but I can confirm that making similar changes fixes the problem. – starfry Apr 21 '16 at 09:56
  • This does work for me as well. Thanks, @neurodynamic. – vlasiak Mar 23 '18 at 13:00
2

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
Dmitry Shvetsov
  • 651
  • 10
  • 19
  • after some time I think that monkey-patching kaminari is the dirty solution and now I stick with the neurodynamic and athlon-krum solution. – Dmitry Shvetsov Jun 01 '16 at 05:04
1

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