我是Ruby on Rails的新手,经过深入搜索以找到答案,我决定发布我的问题:
根据Akira Matsuda here的精彩git log示例,我已经实现了与Kaminari的Ajax分页。
分页本身效果很好,但' page_entries_info'它显示了正在列出的条目,即"显示收藏夹11 - 20共34个"没有更新。它保持初始值"显示收藏夹1 - 10 of 34 in total"。
我添加了":remote =>真"选项,没有成功。我的代码如下所示:
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>
我渲染了部分&#34; _favorite.html.erb&#34;:
<% @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') %>');
favorites_controller.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
favorite.rb
class Favorite < ActiveRecord::Base
belongs_to :user
belongs_to :lesson
end
非常感谢。
答案 0 :(得分:1)
让我们试试这个
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) %>');
答案 1 :(得分:1)
我通过在index.js.erb中添加以下代码找到了解决方案:
$('#counter').html('<%= escape_javascript(page_entries_info(@favorites, :remote => true).to_s) %>');
并添加id =&#34; counter&#34;到index.html.erb中的div:
<div id="counter"class="pagination_count pull-left">
<%= page_entries_info @favorites, :remote => true %>
</div>
答案 2 :(得分:0)
Kaminari使用Active Record的偏移方法创建page_entries_info
文本。只需在查询中添加订单范围即可解决此问题。
class Favorite < ActiveRecord::Base
belongs_to :user
belongs_to :lesson
default_scope ->{ order('id) }
end