简体   繁体   English

如何在 Rails 6 应用程序中为 Pagy Gem 启用奇特路由

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

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 .我正在尝试将 Rails 6 应用程序中所有分页页面的?page=替换为/page/2 ,这样我就可以根据本指南使用 Page Caching 。

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.我正在使用 Pagy gem 进行分页, 根据他们的文档,我应该能够添加一个辅助方法来覆盖以添加对花哨路线的支持。

I've added this block to my application_helper.rb我已将此块添加到我的 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?关于如何解决这个问题的任何想法?

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

Did you include Pagy::Frontend in application_helper.rb ?您是否在application_helper.rbinclude Pagy::Frontend

# 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

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. __pagy_page__是在前端和许多扩展中使用的占位符,因此如果没有完整的回溯,您报告的错误将毫无用处。

Besides posting the backtrace here, you can also post your question in the specific Pagy Support for a faster answer.除了在此处发布回溯,您还可以在特定的Pagey Support中发布您的问题以获得更快的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM