简体   繁体   English

Rails:在Micropost下评论分页

[英]Rails: Comment Pagination under a Micropost

I have a comment model under a micropost. 我在微博下有一个评论模型。 I was wondering if I can paginate this so that 50 comments won't show all at once once the comment button is clicked on the micropost. 我想知道我是否可以对此进行分页,这样一旦在微博上点击评论按钮,就不会同时显示50条评论。 I am pretty sure this cannot be paginated but is there a way that like facebook, only a certain amount shows and then the user can show more if they please. 我很确定这不能分页,但有一种方式像Facebook,只有一定数量的显示,然后用户可以显示更多,如果他们喜欢。 Currently my code looks like this: 目前我的代码如下所示:

Micropost HTML for the Comment Section 评论部分的Micropost HTML

<div id='CommentContainer-<%= micropost.id%>' class='CommentContainer Condensed2'>
<div class='Comment'>
<%= render :partial => "comments/form", :locals => { :micropost => micropost } %>
</div>
<div id='comments'>
<%=render micropost.comments %>
</div>
</div>

When a button above is clicked it shows this section and I would not like 50 comments to show up all at once. 单击上面的按钮时会显示此部分,我不希望一次显示50条评论。 All suggestions will be very helpful. 所有建议都非常有用。 Thank you! 谢谢!

  1. Make sure you are calling @comments = Comment.paginate(:page => params[:page], :per_page => 10) in your controller 确保在@comments = Comment.paginate(:page => params[:page], :per_page => 10)中调用@comments = Comment.paginate(:page => params[:page], :per_page => 10)
  2. You render the group through the <%= will_paginate @comments %> call and not through :render and then later through will_paginate 您通过<%= will_paginate @comments %>调用渲染组,而不是通过:render然后通过will_paginate
  3. Since I already know you are using nested partials. 因为我已经知道你正在使用嵌套的部分。 I would make sure that the item you are calling has the correct collection. 我会确保您呼叫的项目具有正确的集合。 After those three things are met, it should be working fine. 在满足这三个方面之后,应该可以正常工作。

You can also be sure to checkout the railscast on will_paginate although without a pro subscription you may have to watch the outdated version . 您还可以确保在will_paginate上结束railscast,虽然没有专业订阅,您可能需要观看过时的版本

I would suggest you to lookup 2 rails gems that are most widely used for pagination on rails 我建议你查找最常用于轨道分页的2个轨道宝石

--> will paginate - > 会分页

--> Kaminari - > Kaminari

I prefer Kaminari over will paginate because it offers a lot more flexibility than will paginate. 我更喜欢Kaminari而不是paginate因为它提供了比分页更多的灵活性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM