ruby-on-rails - Kaminari 的 Ajax 分页工作正常,但如何更新 page_entries_info?

标签 ruby-on-rails ruby ajax view kaminari

我是 Ruby on Rails 的新手,经过深入搜索以找到答案后,我决定发布我的问题:

我已经根据 Akira Matsuda here 的精彩 git 日志示例,使用 Kaminari 实现了 Ajax 分页。 。

分页本身运行良好,但显示正在列出哪些条目的“page_entries_info”(即“总共显示 34 个收藏夹中的 11 - 20 个”)不会更新。它保持初始值“显示收藏夹 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') %>');

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

最喜欢的.rb

class Favorite < ActiveRecord::Base
  belongs_to :user
  belongs_to :lesson
end

非常感谢。

最佳答案

让我们试试这个

索引.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) %>');

关于ruby-on-rails - Kaminari 的 Ajax 分页工作正常,但如何更新 page_entries_info?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28613442/

相关文章:

ruby-on-rails - 运行功能时不会自动加载 cucumber 步骤

ruby-on-rails - 如何扩展 auth_token 的非常短的到期时间?

php - 哪一端创建 session /cookie?

ruby - 使用iron_worker_ruby gem 时铁 worker 内部的继承

php - Jquery Ajax 返回 NaN

ruby-on-rails - 更新生产中的 ElasticSearch 映射(轮胎)

ruby-on-rails - 如何在 rails 中使用载波 'after the fact' 裁剪图像?

ruby-on-rails - 如何从 Rails 模板(ERB 文件)中的错误中拯救出来

javascript - jQuery 无限页面滚动

javascript - 扩展通过 AJAX 返回的 div