[英]Kaminari, pagination, AJAX strange behaviour
I managed to get my AJAX page load working, but I run into something I don't know how to fix: I set number of entries per page to be 4. On first page it loads 4 items alright but on the next page, it loads more than 4 items.我设法让我的 AJAX 页面加载工作,但我遇到了一些我不知道如何解决的问题:我将每页的条目数设置为 4。在第一页上它加载了 4 个项目,但在下一页上,它加载超过 4 个项目。 Why's that?为什么?
live site: https://serene-waters-1174.herokuapp.com/直播网站: https : //serene-waters-1174.herokuapp.com/
I checked the logs and I realised the OFFSET
is not staying consistent after each ajax load.我检查了日志,发现每次 ajax 加载后OFFSET
都不一致。 ON page load OFFSET
is for, like I want, on subsequent loads, the OFFSET
is 0
. ON 页面加载OFFSET
就像我想要的那样,在后续加载中, OFFSET
为0
。
Started GET "/users/index?page=2" for ::1 at 2015-06-18 21:17:29 +0200
Processing by UsersController#index as JS
Parameters: {"page"=>"2"}
User Load (1.0ms) SELECT "users".* FROM "users" ORDER BY "users"."name" ASC LIMIT 4 OFFSET 4
Rendered users/_user.html.erb (0.3ms)
(0.2ms) SELECT COUNT(*) FROM "users"
Rendered users/index.js.erb (35.3ms)
Completed 200 OK in 45ms (Views: 42.5ms | ActiveRecord: 1.1ms)
Started GET "/users/index" for ::1 at 2015-06-18 21:17:34 +0200
Processing by UsersController#index as JS
User Load (0.8ms) SELECT "users".* FROM "users" ORDER BY "users"."name" ASC LIMIT 4 OFFSET 0
Rendered users/_user.html.erb (0.9ms)
(0.2ms) SELECT COUNT(*) FROM "users"
Rendered users/index.js.erb (48.3ms)
Completed 200 OK in 59ms (Views: 57.1ms | ActiveRecord: 1.0ms)
class User < ActiveRecord::Base
paginates_per 4
end
class UsersController < ApplicationController
def index
@users = User.order(:name).page params[:page]
respond_to do |format|
format.html
format.js {}
end
end
def show
@user = User.find(params[:id])
end
end
<h1>All users in database.</h1>
<div id="paginator">
<%= paginate @users, :remote => true %>
</div><br>
<div id="list">
<%= render 'user' %>
</div>
$('#list').html('<%= escape_javascript render(@users) %>');
$('#paginator').html('<%= escape_javascript(paginate(@users, :remote => true).to_s) %>');
I will appreciate some help with getting my "next" links to load page with ajax.我将不胜感激,帮助我获得“下一个”链接以使用 ajax 加载页面。
EDIT: I realised from the page source that no all pages only 4 items are being shown.编辑:我从页面源中意识到没有所有页面只显示 4 个项目。 Perfect.完美的。 But in the view, it shows 4 items, and repeats those same items 4 times.但是在视图中,它显示了 4 个项目,并且这些相同的项目重复了 4 次。
EDIT:编辑:
This fixes it in the partial.这在部分修复了它。 I was previously using a .each
loop on the partial.我以前在部分上使用.each
循环。
<%= "Name: #{user.name}" %> <br>
<%= "Email: #{user.email}" %><br>
<%= "Album: #{user.album}" %>
<p>======</p>
The OFFSET
is governed by the params[:page]
- and as you can see in your logs the :page
parameter is not being sent through on the second request. OFFSET
由params[:page]
- 正如您在日志中看到的那样,第二个请求没有发送:page
参数。
However, none of that would explain why more than 4 users are being shown on a page, both SQL queries show the correct LIMIT 4
so perhaps you have another error in your JS for updating the DOM or maybe in your user partial and it's adding the users instead of replacing them.然而,这些都不能解释为什么一个页面上显示了超过 4 个用户,两个 SQL 查询都显示了正确的LIMIT 4
所以也许你的 JS 中有另一个错误来更新 DOM 或者你的用户部分并且它正在添加用户而不是替换他们。
对我.each
是将部分保留为.each
循环,但更改我的 js.erb 文件以呈现部分而不是实例变量。
$('#certs').html('<%= escape_javascript render "certifications/certification" %>');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.