我正在使用Rails和Kaminari,并尝试对“位置和帖子”下面的评论实施无休止的滚动。我的理解很正确,但是当我向下滚动时,它会一遍又一遍地加载相同的注释。如何使其正确加载评论?

这是我的位置/show.js.erb

  1 $('#comments').append('<%= j render(@comments) %>');


这是我的comment.js

  5 jQuery ->
  6   $(window).scroll ->
  7     if $(window).scrollTop() > $(document).height() - $(window).height() - 200
  8       $.getScript($('.pagination .next_page a').attr('href'))


我的评论控制器显示操作

 18   def show
 19     @title = @location.name
 20     @comments = @location.comments.order("created_at desc").page(params[:page]).per(35)
 21     @commentable = @location
 22     @comment = @location.comments.build
 23     respond_to do |format|
 24       format.html
 25       format.js
 26     end
 27   end


编辑:这是分页html源

<div class="pagination">
    <ul>

          <li class="active">
  <a href="#">1</a>
</li>

          <li class="">
  <a href="/locations/1?page=2" rel="next">2</a>
</li>

          <li class="">
  <a href="/locations/1?page=3">3</a>
</li>

          <li class="">
  <a href="/locations/1?page=4">4</a>
</li>

          <li class="">
  <a href="/locations/1?page=5">5</a>
</li>

      <li>
  <a href="/locations/1?page=5">Last &raquo;</a>
</li>

    </ul>
  </div>

最佳答案

.next_page链接未更改。
链接就是what.com/?page=2。滚动时,它只是在请求同一页面。您将需要Javascript更新链接中的页码,或者不引用此类DOM。

您可以使用以下方法更新分页器:

$('.pagination').html('<%= escape_javascript(paginate(@comments)) %>');


Kaminari为此提供了“操作方法”:How-To:-Create-Infinite-Scrolling-with-jQuery

关于javascript - 无休止的页面一遍又一遍地加载相同的记录,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14912060/

10-11 20:11