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".
Kaminari::Cells paginate method not rendering anything
1.1k Views Asked by neurodynamic AtThere are 3 best solutions below
 On
                        
                            
                        
                        
                            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
 On
                        
                            
                        
                        
                            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
I'm not sure why it works, but athlon-krum suggested removing the
paginator.render doblock of the_paginator.html.erbKaminari view file, changing it from this:to this:
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).