简体   繁体   中英

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. 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. Currently my code looks like this:

Micropost HTML for the Comment Section

<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. 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
  2. You render the group through the <%= will_paginate @comments %> call and not through :render and then later through 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 .

I would suggest you to lookup 2 rails gems that are most widely used for pagination on rails

--> will paginate

--> Kaminari

I prefer Kaminari over will paginate because it offers a lot more flexibility than will paginate.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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