使用 Ajax 的 Rails Kaminari 分页

发布于 2024-12-29 06:45:39 字数 1144 浏览 1 评论 0原文

我在使用 Ajax 进行分页时遇到问题。我的视图代码看起来分页看起来像

= paginate @products, :remote => true

但是,AJAX 调用失败并显示错误状态代码,Web Inspector 显示如下所示 在此处输入图像描述

服务器日志显示

ERROR Errno::ECONNRESET: Connection reset by peer
/Users/user/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/webrick/httpserver.rb:56:in `eof?'
/Users/user/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/webrick/httpserver.rb:56:in `run'
/Users/user/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread'

有趣的是,如果我向同一页面添加一个超链接,例如

= link_to('Test', products_path(:page => 2), :remote => true)

链接的外观与 Kaminari 生成的完全一样,Ajax 调用成功,没有任何问题。更有趣的是,如果我将生成的分页链接更改为

<a href="/brands/deborah-lippmann" data-remote="true" rel="prev">1</a>

<a href="/brands/deborah-lippmann" data-remote="true">1</a>

与 AJAX 无关的链接,该链接将再次正常工作

这是否与 Kaminari 渲染链接之前发生的 jquery-ujs 魔法有关,Kaminari 使用 ActionView 扩展和 ActionView 上下文来生成分页链接的标签?非常感谢有关此问题的任何帮助!

I'm having troubles paginating using Ajax. My View code looks for pagination looks like

= paginate @products, :remote => true

However, the AJAX call fails with error status code and Web Inspector shows it as seen below
enter image description here

The server logs show

ERROR Errno::ECONNRESET: Connection reset by peer
/Users/user/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/webrick/httpserver.rb:56:in `eof?'
/Users/user/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/webrick/httpserver.rb:56:in `run'
/Users/user/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread'

What is interesting though is if I add a hyperlink to the same page like

= link_to('Test', products_path(:page => 2), :remote => true)

The link look exactly like the one generated by Kaminari and the Ajax call succeeds without any issues whatsoever. More interesting, if I change generated pagination link from

<a href="/brands/deborah-lippmann" data-remote="true" rel="prev">1</a>

to

<a href="/brands/deborah-lippmann" data-remote="true">1</a>

which has nothing to do with AJAX, the link works fine again.

Is it something to do with jquery-ujs magic happening before links are rendered by Kaminari which uses ActionView Extensions and ActionView Context to generate tags for pagination links? Any help on this issue is greatly appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

优雅的叶子 2025-01-05 06:45:39

Chek /app/views/layouts/application.html.erb 获取下一行

<%= javascript_include_tag :defaults %>
<%= csrf_meta_tag %>

Chek /app/views/layouts/application.html.erb for next lines

<%= javascript_include_tag :defaults %>
<%= csrf_meta_tag %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文