Ruby 在 Rails 上:will_paginate 不工作。下一页仍然显示所有评论
Ruby on Rails: will_paginate is not working. Next page still showing all comments
所以我不知道我做错了什么,但是我的 will_paginate 方法没有按预期工作。问题是我想设置每页限制,但是所有评论仍然显示在第一页,当我点击下一步按钮时,所有评论仍然显示在第二页。
这是我的 UsersController(请记住用户有很多评论):
def show
@user = User.find(params[:id])
@comments = @user.comments.paginate(page: params[:page], :per_page => 20)
end
这是我的用户显示视图:
<div class="span6">
<h3>Comments (<%= @user.comments.count %>)</h3>
<ol class="user_comments">
<% @user.comments.each do |comment| %>
<li>
<span class="content"><%= comment.content %></span>
<span class="timestamp">
Posted <%= time_ago_in_words(comment.created_at) %> ago to <%= link_to Article.find_by_id(comment.article_id).title, Article.find_by_id(comment.article_id) %>.
</span>
<div>
<% if current_user?(comment.user) %>
<%= link_to "delete", comment, method: :delete,
data: { confirm: "You sure?" },
title: comment.content %>
<% end %>
</div>
</li>
<% end %>
</ol>
<%= will_paginate @comments %>
奇怪的是,我的文章显示视图中有非常相似的代码可以正常工作(文章也有很多评论)...任何帮助将不胜感激!
你的错误很简单:)这一行:
<% @user.comments.each do |comment| %>
应替换为:
<% @comments.each do |comment| %>
因为您没有使用过滤后的数组
所以我不知道我做错了什么,但是我的 will_paginate 方法没有按预期工作。问题是我想设置每页限制,但是所有评论仍然显示在第一页,当我点击下一步按钮时,所有评论仍然显示在第二页。
这是我的 UsersController(请记住用户有很多评论):
def show
@user = User.find(params[:id])
@comments = @user.comments.paginate(page: params[:page], :per_page => 20)
end
这是我的用户显示视图:
<div class="span6">
<h3>Comments (<%= @user.comments.count %>)</h3>
<ol class="user_comments">
<% @user.comments.each do |comment| %>
<li>
<span class="content"><%= comment.content %></span>
<span class="timestamp">
Posted <%= time_ago_in_words(comment.created_at) %> ago to <%= link_to Article.find_by_id(comment.article_id).title, Article.find_by_id(comment.article_id) %>.
</span>
<div>
<% if current_user?(comment.user) %>
<%= link_to "delete", comment, method: :delete,
data: { confirm: "You sure?" },
title: comment.content %>
<% end %>
</div>
</li>
<% end %>
</ol>
<%= will_paginate @comments %>
奇怪的是,我的文章显示视图中有非常相似的代码可以正常工作(文章也有很多评论)...任何帮助将不胜感激!
你的错误很简单:)这一行:
<% @user.comments.each do |comment| %>
应替换为:
<% @comments.each do |comment| %>
因为您没有使用过滤后的数组