Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm using the will_paginate gem and currently have a list that is already paginated.

@businesses = Business.paginate(:page => params[:page], :per_page => 7)

<%= will_paginate @businesses %>

<% @businesses.each do |business| %>

To display the comments for a particular entry I use:

<% @business.comments.each do |comment| %>

I thought something like the following might work but it didn't:

@business.comments = Business.comments.paginate(:page => params[:page], :per_page => 7)

<%= will_paginate @business.comments %>

I can't seem to find any answer to paginating the comments (99.9% sure its to do with it being @something.something instead of just @something)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
297 views
Welcome To Ask or Share your Answers For Others

1 Answer

Setting @business.comments not only won't work but is actually dangerous and destructive. You are setting the comments associated to the business to be only the ones returned by that paginate call. (Check what the log shows when you set @business.comments to the paginated call.

You have, however, got the call right. You just need to set it to a different variable rather than reusing @business.comments. If you use @comments instead it will work fine:

@comments = @business.comments.paginate(:page => params[:page], :per_page => 7)

and

@comments.each do |comment|
  ...
end
will_paginate @comments

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...