# Question:how to paginate comments?

**URL:** <https://www.ruby-forum.com/t/question-how-to-paginate-comments/128502>\
**Category:** Rails\
**Created:** [February 8, 2008, 6:49am UTC](https://www.ruby-forum.com/t/question-how-to-paginate-comments/128502 "2008-02-08T06:49:49Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![flychunjae](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/3ab097/32.png) [@flychunjae](https://www.ruby-forum.com/u/flychunjae)\
**Post date:** [February 8, 2008, 6:49am UTC](https://www.ruby-forum.com/t/question-how-to-paginate-comments/128502/1 "2008-02-08T06:49:49Z")

</div>

hello again,

i’m trying to paginate comments with will\_paginate in order, newest on  
top, and i’m using the sample script from acts\_as\_commentable in my  
controller:

def add\_comment  
commentable\_type = params[:commentable][:commentable]  
commentable\_id = params[:commentable][:commentable\_id]  
# Get the object that you want to comment  
commentable = Comment.find\_commentable(commentable\_type,  
commentable\_id)

```
  # Create a comment with the user submitted content
  comment = Comment.new(params[:comment])
  # Assign this comment to the logged in user
  comment.user_id = current_user

  # Add the comment
  commentable.comments << comment

  redirect_to :action => "show",
  :id => commentable_id

```

end

as well as :

def show\_comment  
@post = Post.paginate :per\_page =\>10, :page =\> params[:page],  
:order =\> “DESC”  
end  
i have a partial that shows just the comments to the post

\<%= @post.comments%\>  
\<%= will\_paginate @post.comments %\>

and i get this msg:  
undefined method `page\_count’ for #Class:0x4a7601c

so my question is how do i paginate comments for specific posts?

thank you guys!

---

<div class="post-metadata">

**Author:** ![flychunjae](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/3ab097/32.png) [@flychunjae](https://www.ruby-forum.com/u/flychunjae)\
**Post date:** [February 8, 2008, 12:30pm UTC](https://www.ruby-forum.com/t/question-how-to-paginate-comments/128502/2 "2008-02-08T12:30:50Z")

</div>

i’m a bit vconfused about your code, do you want to paginate posts and  
“sub-paginate” the comments for each?!?

i guess your show\_comments method should rather get 1 post, and  
paginate the comments for this post.  
it _should_ work like this, though i didn’t test it.

@post = Post.find(params[:id])  
@comments = @post.comments.paginate :per\_page =\> 10, :page =\>  
params[:page], :order =\> " created\_at DESC"  
#…  
\<%= will\_paginate @comments %\>

i use a seperate variabel for the comments, as the paginate method  
doesn’t return a simple array but a will\_paginate object (that extends  
Array if i recall correctly)  
Give it a shot.

On 8 Feb., 06:49, Richard Y. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
