kaminari and ransack_memory

I’m tinkering with an application in Rails and added paging for the indexes with the kaminari/kaminari gem. So far so good. Then I also added sorting and selecting in the index pages with the ransack gem. After some time I noticed that the sorting and selecting configuration would disappear if navigating away from the index. The solution is adding ransack_memory to the fray of gems that help you to get your application running with the features you want. The maintainers of ransack_memory warn against problems when combining it with kaminari. They mention the fact that paging backward is not working and they also provide a possible solution. That alas yielded other problems with Rails 6.1 alas.

So I started tinkering. And I observed that kaminari does not add a URL-parameter in case the target page is the first page of the series. Very tidy indeed. Why specify the page when you really do not need it. Well, it turned out that that is in all probability the reason why the two are not working together. So spelunk around in the source of kaminari put me in the direction of a solution: if you specify that kaminari also needs to add the URL parameters when navigating to the first page, then the problem disappears. So, in the kaminari initializer config/initializers/kaminari_config.rb uncomment config.params_on_first_page and set it to true:

Kaminari.configure do |config|

  ... other configuration items

  # ----------------------------------------------------------------------------
  # If you are using ransack-memory then set params_on_first_page to true
  # otherwise you cannot navicate back or to first page
  config.params_on_first_page = true


Leave a Comment