Rails 5:will_paginate gem 分页重复 objects(ID 1-10 等的 100 objects)。
Rails 5: will_paginate gem paginating repeated objects (100 objects of ids 1-10 etc).
我在我的应用程序上安装了 will_paginate
版本 3.1.5
,它正在做一些奇怪的事情。
我正在尝试分页 Books
。我在 books_controller
:
中设置了这样的分页
def index
@books = Book.paginate(page: params[:page], per_page: 10)
respond_to do |format|
format.html
format.js
end
end
但是当我访问那个页面时,我注意到终端没有像它应该的那样查询 10 本书:
Started GET "/" for ::1 at 2017-05-16 10:28:47 -0700
Processing by BooksController#index as HTML
Rendering books/index.html.erb within layouts/application
Book Load (1.1ms) SELECT "books".* FROM "books" LIMIT OFFSET [["LIMIT", 10], ["OFFSET", 0]]
Rendered collection of books/_book.html.erb [10 times] (291.7ms)
(0.6ms) SELECT COUNT(*) FROM "books"
Rendered books/index.html.erb within layouts/application (389.9ms)
Completed 200 OK in 880ms (Views: 849.9ms | ActiveRecord: 1.7ms)
如果我检查 @books
,我发现 collection 中有 100 个 books
。
(byebug) @books.count
CACHE (0.0ms) SELECT COUNT(*) FROM "books"
100
但是,进一步检查,它只是 collection 中前 10 本书的 10 份。 collection中只有id
是1-10.
(byebug) @books.each { |b| puts b.id }
Book Load (0.9ms) SELECT "books".* FROM "books" LIMIT OFFSET [["LIMIT", 10], ["OFFSET", 0]]
1
2
3
4
5
6
7
8
9
10
如果我转到下一页,我只会得到另外 100 个结果,但是 id
s 只有 11-20:
(byebug) @books.count
(0.4ms) SELECT COUNT(*) FROM "books"
100
(byebug) @books.each { |b| puts b.id }
Book Load (4.9ms) SELECT "books".* FROM "books" LIMIT OFFSET [["LIMIT", 10], ["OFFSET", 10]]
11
12
13
14
15
16
17
18
19
20
以下是我的观点:
<!-- index.html.erb -->
<h1>Books list:</h1>
<div>
<table>
<thead>
<tr>
<th>ID</th>
<th>Title</th>
<th>Author</th>
<th>Genre</th>
<th>Publication Date</th>
</tr>
</thead>
<tbody id="book-list">
<%= render @books %>
</tbody>
</table>
</ul>
</div>
<div id="infinite-scrolling">
<%= will_paginate %>
</div>
<!-- _book.html.erb -->
<% @books.each do |b| %>
<tr>
<div>
<td><%= b.id %></td>
<td><%= b.title %></td>
<td><%= b.author %></td>
<td><%= b.genre %></td>
<td><%= b.pub_date %></td>
</div>
</tr>
<% end %>
我解决了我的问题。我处理的代码 <%= render @books %>
错误(我用得不多)。
我不得不更改部分代码以使其更加隐含。使用 @books.each do |b|
会使事情重复多次。将我的代码更改为:
<!-- _book.html.erb -->
<tr>
<div>
<td><%= book.id %></td>
<td><%= book.title %></td>
<td><%= book.author %></td>
<td><%= book.genre %></td>
<td><%= book.pub_date %></td>
</div>
</tr>
解决了我所有的问题。
我在我的应用程序上安装了 will_paginate
版本 3.1.5
,它正在做一些奇怪的事情。
我正在尝试分页 Books
。我在 books_controller
:
def index
@books = Book.paginate(page: params[:page], per_page: 10)
respond_to do |format|
format.html
format.js
end
end
但是当我访问那个页面时,我注意到终端没有像它应该的那样查询 10 本书:
Started GET "/" for ::1 at 2017-05-16 10:28:47 -0700
Processing by BooksController#index as HTML
Rendering books/index.html.erb within layouts/application
Book Load (1.1ms) SELECT "books".* FROM "books" LIMIT OFFSET [["LIMIT", 10], ["OFFSET", 0]]
Rendered collection of books/_book.html.erb [10 times] (291.7ms)
(0.6ms) SELECT COUNT(*) FROM "books"
Rendered books/index.html.erb within layouts/application (389.9ms)
Completed 200 OK in 880ms (Views: 849.9ms | ActiveRecord: 1.7ms)
如果我检查 @books
,我发现 collection 中有 100 个 books
。
(byebug) @books.count
CACHE (0.0ms) SELECT COUNT(*) FROM "books"
100
但是,进一步检查,它只是 collection 中前 10 本书的 10 份。 collection中只有id
是1-10.
(byebug) @books.each { |b| puts b.id }
Book Load (0.9ms) SELECT "books".* FROM "books" LIMIT OFFSET [["LIMIT", 10], ["OFFSET", 0]]
1
2
3
4
5
6
7
8
9
10
如果我转到下一页,我只会得到另外 100 个结果,但是 id
s 只有 11-20:
(byebug) @books.count
(0.4ms) SELECT COUNT(*) FROM "books"
100
(byebug) @books.each { |b| puts b.id }
Book Load (4.9ms) SELECT "books".* FROM "books" LIMIT OFFSET [["LIMIT", 10], ["OFFSET", 10]]
11
12
13
14
15
16
17
18
19
20
以下是我的观点:
<!-- index.html.erb -->
<h1>Books list:</h1>
<div>
<table>
<thead>
<tr>
<th>ID</th>
<th>Title</th>
<th>Author</th>
<th>Genre</th>
<th>Publication Date</th>
</tr>
</thead>
<tbody id="book-list">
<%= render @books %>
</tbody>
</table>
</ul>
</div>
<div id="infinite-scrolling">
<%= will_paginate %>
</div>
<!-- _book.html.erb -->
<% @books.each do |b| %>
<tr>
<div>
<td><%= b.id %></td>
<td><%= b.title %></td>
<td><%= b.author %></td>
<td><%= b.genre %></td>
<td><%= b.pub_date %></td>
</div>
</tr>
<% end %>
我解决了我的问题。我处理的代码 <%= render @books %>
错误(我用得不多)。
我不得不更改部分代码以使其更加隐含。使用 @books.each do |b|
会使事情重复多次。将我的代码更改为:
<!-- _book.html.erb -->
<tr>
<div>
<td><%= book.id %></td>
<td><%= book.title %></td>
<td><%= book.author %></td>
<td><%= book.genre %></td>
<td><%= book.pub_date %></td>
</div>
</tr>
解决了我所有的问题。