Rails 5:Kaminari gem 设置 ajax

Rails 5: Kaminari gem with ajax setup

我已经安装了 kaminari gem,它与我的 rails 5 应用程序配合使用时运行良好。在我有评论的页面上,我试图通过 Ajax 实现分页,因此不刷新页面。评论位于带有标签的面板内。

这是我目前的设置:

gem 'kaminari'

控制器动作

 def show
  @item = Item.find(params[:id])
  @comments = Comment.where(item_id: @item).order(created_at: :desc).page(params[:page])

  respond_to do |format|
    format.js  { render 'show.js.erb' }
    format.html
  end
end

show.js.erb

$("#pages-comments").html("<%= escape_javascript render(@comments) %>")
$("#paginator").html("<%= escape_javascript(paginate(@comments, remote: true)) %>")

show.html.erb

<div class="tab-pane" id="comments">
     <div class="pages-comments">
        <%= render 'comments/comment' %>
      </div>

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

 </div>

即使我点击分页链接时 show.js.erb 文件似乎正在呈现,它也不会将我重定向到下一页。另外,我在浏览器检查器中没有收到任何错误。

这是使用 byebug 时控制台显示的内容:

  Rendering pages/show.js.erb
  Comment Load (1.8ms)  SELECT  "comments".* FROM "comments" WHERE "comments"."item_id" = 8 ORDER BY "comments"."created_at" DESC LIMIT  OFFSET   [["LIMIT", 10], ["OFFSET", 0]]
   (1.3ms)  SELECT COUNT(*) FROM (SELECT  1 FROM "comments" WHERE "comments"."item_id" = 8 ORDER BY "comments"."created_at" DESC LIMIT  OFFSET ) subquery_for_count  [["LIMIT", 10], ["OFFSET", 0]]
  Shopper Load (1.2ms)  SELECT  "shoppers".* FROM "shoppers" WHERE "shoppers"."id" =  LIMIT   [["id", 2], ["LIMIT", 1]]
  CACHE Shopper Load (0.2ms)  SELECT  "shoppers".* FROM "shoppers" WHERE "shoppers"."id" =  LIMIT   [["id", 2], ["LIMIT", 1]]
  CACHE Shopper Load (0.3ms)  SELECT  "shoppers".* FROM "shoppers" WHERE "shoppers"."id" =  LIMIT   [["id", 2], ["LIMIT", 1]]
  CACHE Shopper Load (0.2ms)  SELECT  "shoppers".* FROM "shoppers" WHERE "shoppers"."id" =  LIMIT   [["id", 2], ["LIMIT", 1]]
  CACHE Shopper Load (0.4ms)  SELECT  "shoppers".* FROM "shoppers" WHERE "shoppers"."id" =  LIMIT   [["id", 2], ["LIMIT", 1]]
  CACHE Shopper Load (0.2ms)  SELECT  "shoppers".* FROM "shoppers" WHERE "shoppers"."id" =  LIMIT   [["id", 2], ["LIMIT", 1]]
  CACHE Shopper Load (0.5ms)  SELECT  "shoppers".* FROM "shoppers" WHERE "shoppers"."id" =  LIMIT   [["id", 2], ["LIMIT", 1]]
  CACHE Shopper Load (0.3ms)  SELECT  "shoppers".* FROM "shoppers" WHERE "shoppers"."id" =  LIMIT   [["id", 2], ["LIMIT", 1]]
  CACHE Shopper Load (0.3ms)  SELECT  "shoppers".* FROM "shoppers" WHERE "shoppers"."id" =  LIMIT   [["id", 2], ["LIMIT", 1]]
  CACHE Shopper Load (1.1ms)  SELECT  "shoppers".* FROM "shoppers" WHERE "shoppers"."id" =  LIMIT   [["id", 2], ["LIMIT", 1]]
  CACHE  (0.2ms)  SELECT COUNT(*) FROM (SELECT  1 FROM "comments" WHERE "comments"."item_id" = 8 ORDER BY "comments"."created_at" DESC LIMIT  OFFSET ) subquery_for_count  [["LIMIT", 10], ["OFFSET", 0]]
  CACHE  (0.2ms)  SELECT COUNT(*) FROM (SELECT  1 FROM "comments" WHERE "comments"."item_id" = 8 ORDER BY "comments"."created_at" DESC LIMIT  OFFSET ) subquery_for_count  [["LIMIT", 10], ["OFFSET", 0]]
  CACHE  (0.2ms)  SELECT COUNT(*) FROM (SELECT  1 FROM "comments" WHERE "comments"."item_id" = 8 ORDER BY "comments"."created_at" DESC LIMIT  OFFSET ) subquery_for_count  [["LIMIT", 10], ["OFFSET", 0]]
  CACHE  (0.2ms)  SELECT COUNT(*) FROM (SELECT  1 FROM "comments" WHERE "comments"."item_id" = 8 ORDER BY "comments"."created_at" DESC LIMIT  OFFSET ) subquery_for_count  [["LIMIT", 10], ["OFFSET", 0]]
  CACHE  (0.2ms)  SELECT COUNT(*) FROM (SELECT  1 FROM "comments" WHERE "comments"."item_id" = 8 ORDER BY "comments"."created_at" DESC LIMIT  OFFSET ) subquery_for_count  [["LIMIT", 10], ["OFFSET", 0]]
  CACHE  (0.2ms)  SELECT COUNT(*) FROM (SELECT  1 FROM "comments" WHERE "comments"."item_id" = 8 ORDER BY "comments"."created_at" DESC LIMIT  OFFSET ) subquery_for_count  [["LIMIT", 10], ["OFFSET", 0]]
  CACHE  (0.2ms)  SELECT COUNT(*) FROM (SELECT  1 FROM "comments" WHERE "comments"."item_id" = 8 ORDER BY "comments"."created_at" DESC LIMIT  OFFSET ) subquery_for_count  [["LIMIT", 10], ["OFFSET", 0]]
  CACHE  (0.3ms)  SELECT COUNT(*) FROM (SELECT  1 FROM "comments" WHERE "comments"."item_id" = 8 ORDER BY "comments"."created_at" DESC LIMIT  OFFSET ) subquery_for_count  [["LIMIT", 10], ["OFFSET", 0]]
  CACHE  (0.2ms)  SELECT COUNT(*) FROM (SELECT  1 FROM "comments" WHERE "comments"."item_id" = 8 ORDER BY "comments"."created_at" DESC LIMIT  OFFSET ) subquery_for_count  [["LIMIT", 10], ["OFFSET", 0]]
  Rendered collection of comments/_comment.html.erb [10 times] (822.0ms)
   (1.0ms)  SELECT COUNT(*) FROM "comments" WHERE "comments"."item_id" = 8
  Rendered pages/show.js.erb (1205.3ms)

知道我做错了什么吗?

更新 1 在执行@Gabbar 回答后,当我点击下一页时,我得到了这个:

Started GET "/show/8?page=2" for 127.0.0.1 at 2017-12-01 16:51:39 +0200
Processing by PagesController#show as JS
  Parameters: {"page"=>"2", "id"=>"8"}
  Item Load (0.5ms)  SELECT  "items".* FROM "items" WHERE "items"."id" =  LIMIT   [["id", 8], ["LIMIT", 1]]
Completed 500 Internal Server Error in 3ms (ActiveRecord: 0.5ms)



TypeError (no implicit conversion of Integer into String):

app/controllers/pages_controller.rb:33:in `+'
app/controllers/pages_controller.rb:33:in `show'

你能试试这个吗

 def show
  params[:page] ||= 1
  @item = Item.find(params[:id])
  @comments = @item.comments.order(created_at: :desc).page(params[:page])
  #if params[:page].class == String
    #params[:page].to_i = params[:page].to_i + 1
  #else
    #params[:page] = params[:page] + 1
  #end
  respond_to do |format|
    format.js  { render 'show.js.erb' }
    format.html
  end
end

在show.js.erb

$(".pages-comments").html("<%= escape_javascript render(@comments) %>"); 
$("#paginator").html("<%= escape_javascript(paginate(@comments, remote: true)) %>");