1

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?

tommers
  • 51
  • 7

3 Answers3

1

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
Fallenhero
  • 1,563
  • 1
  • 8
  • 17
  • The documentation for the current version is correct, but it was missing the comment about the inverted order of the params in previous versions. Added. Thanks. – user712557 Jun 05 '21 at 12:28
0

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
eux
  • 3,072
  • 5
  • 14
0

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.

user712557
  • 327
  • 2
  • 5