Search code examples
ruby-on-railsrubypaginationkaminari

Always show pagination controls for Kaminari


Is there a way to make Kaminari always show pagination controls even when total page count is 1?


Solution

  • You can extend Kaminari at run-time, a process known as [monkey-patching]. Just create a file in config/initializers/kaminari_ext.rb with the following:

    module Kaminari
      module Helpers
        class Paginator
          def render(&block)
            instance_eval(&block) if @options[:total_pages] >= 1
            @output_buffer
          end
        end
      end
    end
    

    You will need to restart your rails server for the changes to take effect.

    If you get an error about num_pages being nil you can change num_pages within a theme, changing it to total_pages. Eventually num_pages will be deprecated in lieu of total_pages.

    For example, I had to change _page.html.haml:

    = raw("PAGE  #{page}  OF  #{total_pages}")