简体   繁体   中英

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. Why's that?

live site: https://serene-waters-1174.herokuapp.com/

I checked the logs and I realised the OFFSET is not staying consistent after each ajax load. ON page load OFFSET is for, like I want, on subsequent loads, the OFFSET is 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)

user.rb

class User < ActiveRecord::Base
      paginates_per 4
end

user_controller.rb

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

index.html.erb

<h1>All users in database.</h1>

<div id="paginator">
    <%= paginate @users, :remote => true %>
</div><br>

<div id="list">
    <%= render 'user' %>
</div>

index.js.erb

$('#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.

EDIT: I realised from the page source that no all pages only 4 items are being shown. Perfect. But in the view, it shows 4 items, and repeats those same items 4 times.

EDIT:

This fixes it in the partial. I was previously using a .each loop on the partial.

<%= "Name: #{user.name}" %> <br>
<%= "Email: #{user.email}" %><br>
<%= "Album: #{user.album}" %>
<p>======</p>

Thanks to smathy !

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.

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.

对我.each是将部分保留为.each循环,但更改我的 js.erb 文件以呈现部分而不是实例变量。

$('#certs').html('<%= escape_javascript render "certifications/certification" %>');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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