How to enable fancy-routes for Pagy Gem in Rails 6 app

778 Views Asked by At

I'm trying to replace ?page= with /page/2 for all paginated pages in my Rails 6 application, so that I can be able to use Page Caching according to this guide.

I'm using the Pagy gem for pagination and according to their documentation, I should be able to add a helper method to override to add support for fancy routes.

I've added this block to my application_helper.rb

def pagy_url_for(pagy, page)
  params = request.query_parameters.merge(pagy.vars[:page_param] => page )
  url_for(params)
end

However, after I do that I get this error:

 "undefined method `vars' for "__pagy_page__":String"

Any ideas on how to solve that?

3

There are 3 best solutions below

0
eux On

Did you include Pagy::Frontend in application_helper.rb?

# application_helper.rb
class ApplicationHelper
  include Pagy::Frontend

  def pagy_url_for(pagy, page)
    params = request.query_parameters.merge(pagy.vars[:page_param] => page )
    url_for(params)
  end
end
0
user712557 On

The __pagy_page__ is a placeholder used in the frontend and in a number of extensions so the error that you report is not useful without a full backtrace.

Besides posting the backtrace here, you can also post your question in the specific Pagy Support for a faster answer.

1
Fallenhero On

I think they mixed up the params in their tutorial. Try

def pagy_url_for(page, pagy)
  params = request.query_parameters.merge(pagy.vars[:page_param] => page )
  url_for(params)
end