How to force Kaminari to always include page param?
Asked Answered
C

4

11

Kaminari's URL generation omits the page param if it is generating the link back to the first page. However, the application is designed to select a random page if the page parameter is omitted. Kaminari's default behaviour, then, precludes paginating back to the first page in a reliable way.

I've resolved this issue, and will post my solution below a bit later, but I wanted to post this question for posterity, and I'm also pretty new to Rails, thus I'm not sure my solution is the best or most elegant, and I'm interested in improvements and refinements, if just for my own selfish edification!

Chihuahua answered 30/3, 2011 at 14:58 Comment(0)
C
15

The line of code in Kaminari that implements the behaviour we want to change is in lib/kaminari/helpers/tags.rb, in the method Kaminari::Helpers::Tag::page_url_for.

  def page_url_for(page)
    @template.url_for @template.params.merge(@param_name => (page <= 1 ? nil : page))
  end

To override this behaviour, I created a file lib/kaminari/helpers/tag.rb, containing the following:

module Kaminari
  module Helpers
    class Tag
      def page_url_for(page)
        @template.url_for @template.params.merge(@param_name => (page < 1 ? nil : page))
      end
    end
  end
end

I then patched in the file by adding the following line to config/initializers/extensions.rb:

require "lib/kaminari/helpers/tag.rb"

My apologies for any awkwardness with the Ruby/Rails terminology, I'm still fairly new to Ruby. Comments and criticisms are welcome.

Chihuahua answered 30/3, 2011 at 17:1 Comment(1)
Thanks for you answer, this helped me. I just have an amendment, in Rails 3+ you don't include lib in the require path. It should be require "kaminari/helpers/tag.rb"Sacco
R
8

UPDATE

The new version of the kaminari source will require this as the updated line:

@template.url_for @params.merge(@param_name => (page))

Otherwise you will lose other params passed into your pagination call.

For clairity sake here is the full output of the new code:

module Kaminari
  module Helpers
    class Tag
      def page_url_for(page)
        @template.url_for @params.merge(@param_name => (page))
      end
    end
  end
end

You will still place this inside an initializers file as Daniel suggested.

Rademacher answered 27/7, 2012 at 21:57 Comment(2)
Please specify the kaminari version when the code changes. I need to version proof this. Thanks.Aleishaalejandra
I added this directly on the Kaminari config initializer, works like a charmRusell
P
6

As of today (July 2016), the Kaminari master branch includes a config option params_on_first_page, which is false by default.

Setting this config option to true will include page params for all pages, including page 1.

Note that the master branch isn't a stable release, so use with caution!

Pryer answered 30/7, 2016 at 16:57 Comment(0)
M
4

This is the answer for 2018 as am writing this :

Like it's stated in the kaminari github home page

Run this to create a config file for kaminari :

rails g kaminari:config

This will create a file kaminari_config.rb in your config/initializers folder

Uncomment the line : config.params_on_first_page = false and replace false by true :

config.params_on_first_page = true

Restart your server if necessary. That's it :)

Merl answered 1/2, 2018 at 12:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.