使用 will_paginate 对评论进行分页 [英] Paginate Comments With will_paginate
问题描述
我正在使用 will_paginate
gem 并且当前有一个已经分页的列表.
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 %>
我似乎找不到对评论进行分页的任何答案(99.9% 肯定这与 @something.something 而不是 @something 有关系)
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)
推荐答案
设置 @business.comments
不仅不起作用,而且实际上是危险和破坏性的.您将与业务相关的评论设置为仅该分页调用返回的评论.(检查将 @business.comments
设置为分页调用时日志显示的内容.
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.
但是,您的电话是正确的.您只需将其设置为不同的变量,而不是重复使用 @business.comments
.如果您改用@comments,它将正常工作:
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)
和
@comments.each do |comment|
...
end
will_paginate @comments
这篇关于使用 will_paginate 对评论进行分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!