Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
304 views
in Technique[技术] by (71.8m points)

ruby on rails - Paginate Comments With will_paginate

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

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

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...