我在我的rails应用程序中使用了will_paginate gem,在我的文章中我有评论,所以当用户第一次显示一篇文章时,他只需3条第一条评论就可以看到这篇文章,然后如果他单击一个显示更多的链接(这是一个远程链接/“ajax”),他将看到接下来的8个注释,我尝试使用
在我的文章中,我呈现了一个显示评论的部分,如下所示:
= render partial: "shared/comments", collection: article.comments.paginate(page: params[:page], per_page: 3) # i use paginate to show just 3 comments
= link_to 'Show more', article_comments_path(article.id, :page => 2), :remote => true
如果用户单击“显示更多”链接,ajax请求将触发注释控制器中的“我的索引操作”(我想在这里显示下面的8个元素):
def index
@comments = @commentable.comments.paginate(page: params[:page], per_page: 8)
end
但是它不能正常工作,当我第一次单击“显示更多”时,它跳过了5个元素(8-3),然后显示了接下来的8个元素。
有办法解决这个问题吗?
发布于 2013-11-28 15:58:37
在呈现第2页的记录之前,我通过检索will_paginate跳过的5条记录来解决我的问题。
在我的index.js.erb中:
<% if @comments.current_page == 2 %>
<% @x = @commentable.comments.offset(3).limit(2) # get the 5 skipped records %>
<% else %>
<% @x = "" %>
<% end %>
$("div#comments").append("<%= escape_javascript(render partial: 'shared/comments', collection: @x) %>")
$("div#comments").append("<%= escape_javascript(render partial: 'shared/comments', collection: @comments) %>")
如果你有其他的建议,我会感激的:)
发布于 2013-11-28 04:16:52
我不确定您的index
操作是否也用于其他地方,但您可以尝试:
def index
@comments = @commentable.comments.offset(3).paginate(page: params[:page], per_page: 8)
end
您的链接应该呈现第1页:
= link_to 'Show more', article_comments_path(article.id, :page => 1), :remote => true
https://stackoverflow.com/questions/20257053
复制相似问题