我已经安装了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 $1 OFFSET $2  [["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 $1 OFFSET $2) subquery_for_count  [["LIMIT", 10], ["OFFSET", 0]]
  Shopper Load (1.2ms)  SELECT  "shoppers".* FROM "shoppers" WHERE "shoppers"."id" = $1 LIMIT $2  [["id", 2], ["LIMIT", 1]]
  CACHE Shopper Load (0.2ms)  SELECT  "shoppers".* FROM "shoppers" WHERE "shoppers"."id" = $1 LIMIT $2  [["id", 2], ["LIMIT", 1]]
  CACHE Shopper Load (0.3ms)  SELECT  "shoppers".* FROM "shoppers" WHERE "shoppers"."id" = $1 LIMIT $2  [["id", 2], ["LIMIT", 1]]
  CACHE Shopper Load (0.2ms)  SELECT  "shoppers".* FROM "shoppers" WHERE "shoppers"."id" = $1 LIMIT $2  [["id", 2], ["LIMIT", 1]]
  CACHE Shopper Load (0.4ms)  SELECT  "shoppers".* FROM "shoppers" WHERE "shoppers"."id" = $1 LIMIT $2  [["id", 2], ["LIMIT", 1]]
  CACHE Shopper Load (0.2ms)  SELECT  "shoppers".* FROM "shoppers" WHERE "shoppers"."id" = $1 LIMIT $2  [["id", 2], ["LIMIT", 1]]
  CACHE Shopper Load (0.5ms)  SELECT  "shoppers".* FROM "shoppers" WHERE "shoppers"."id" = $1 LIMIT $2  [["id", 2], ["LIMIT", 1]]
  CACHE Shopper Load (0.3ms)  SELECT  "shoppers".* FROM "shoppers" WHERE "shoppers"."id" = $1 LIMIT $2  [["id", 2], ["LIMIT", 1]]
  CACHE Shopper Load (0.3ms)  SELECT  "shoppers".* FROM "shoppers" WHERE "shoppers"."id" = $1 LIMIT $2  [["id", 2], ["LIMIT", 1]]
  CACHE Shopper Load (1.1ms)  SELECT  "shoppers".* FROM "shoppers" WHERE "shoppers"."id" = $1 LIMIT $2  [["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 $1 OFFSET $2) 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 $1 OFFSET $2) 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 $1 OFFSET $2) 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 $1 OFFSET $2) 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 $1 OFFSET $2) 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 $1 OFFSET $2) 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 $1 OFFSET $2) 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 $1 OFFSET $2) 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 $1 OFFSET $2) 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" = $1 LIMIT $2  [["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)) %>");

关于javascript - Rails 5:Kaminari gem与Ajax设置,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47593723/

10-09 23:30