我是RubyonRails的新手,在这里进行了深入的搜索以找到答案后,我决定发布我的问题:
根据Akira Matsuda的git日志示例,我已经用Kaminari实现了Ajax分页。
分页本身效果很好,但显示列出哪些条目的"page_entries_info",即"显示收藏夹11-20(共34个)"不会更新。它保持初始值"显示收藏夹1-10(共34个)"。
我添加了":remote=>true"选项,但没有成功。我的代码如下:
index.html.erb
<h1>All Favorites</h1>
<div id="paginator" class="pagination_div center">
<%= paginate @favorites, :remote => true %>
</div>
<div class="pagination_count pull-left">
<%= page_entries_info @favorites, :remote => true %>
</div>
<table class="search_results table table-hover">
<thead>
<tr>
<th>ID</th>
<th>User Id</th>
<th>User email</th>
<th>Lesson Id</th>
<th>Lesson name</th>
<th></th>
</tr>
</thead>
<tbody id="favorites">
<%= render 'favorite' %>
</tbody>
</table>
我呈现部分"_favorite.html.erb":
<% @favorites.each do |favorite| %>
<tr>
<td><%= favorite.id %></td>
<td><%= favorite.user_id %></td>
<td><%= favorite.user.email %></td>
<td><%= favorite.lesson_id %></td>
<td><%= favorite.lesson.name %></td>
<td><img class="delete_favorite" src="<%=asset_path('trash.png')%>" lid="<%= favorite.lesson_id %>"></td>
</tr>
<% end %>
这是我的index.js.erb:
$('#paginator').html('<%= escape_javascript(paginate(@favorites, :remote => true).to_s) %>');
$('#favorites').html('<%= escape_javascript render('favorite') %>');
收藏夹_控制器.rb
class FavoritesController < ApplicationController
respond_to :json
load_and_authorize_resource
before_action :verify_if_logged_in
def index
@favorites = Favorite.all.page(params[:page])
end
...
end
收藏夹.rb
class Favorite < ActiveRecord::Base
belongs_to :user
belongs_to :lesson
end
非常感谢。
让我们试试这个
Index.erb
<tbody id="favorites">
<%= render @favorites %>
</tbody>
index.js.erb
$('tbody#favorites').html('<%= escape_javascript render(@favorites) %>');
$('#paginator').html('<%= escape_javascript(paginate(@favorites, :remote => true).to_s) %>');
我通过在index.js.erb:中添加以下代码找到了解决方案
$('#counter').html('<%= escape_javascript(page_entries_info(@favorites, :remote => true).to_s) %>');
并将id="counter"添加到index.html.erb:中的div
<div id="counter"class="pagination_count pull-left">
<%= page_entries_info @favorites, :remote => true %>
</div>
page_entries_info
文本。只要在查询中添加一个订单范围就可以解决这个问题。
class Favorite < ActiveRecord::Base
belongs_to :user
belongs_to :lesson
default_scope ->{ order('id) }
end