Ruby on rails RubyonRails:will_paginate不工作。下一页仍然显示所有评论

Ruby on rails RubyonRails:will_paginate不工作。下一页仍然显示所有评论,ruby-on-rails,ruby,will-paginate,Ruby On Rails,Ruby,Will Paginate,所以我不知道我在这里做错了什么,但是我的will_paginate方法没有按预期工作。问题是,我想设置每页的限制,但所有注释仍显示在第一页,当我单击“下一步”按钮时,所有注释仍显示在第二页 这是我的UsersController(请记住,用户有很多评论): 以下是我的用户显示视图: <div class="span6"> <h3>Comments (<%= @user.comments.count %>)</h3> <ol class

所以我不知道我在这里做错了什么,但是我的will_paginate方法没有按预期工作。问题是,我想设置每页的限制,但所有注释仍显示在第一页,当我单击“下一步”按钮时,所有注释仍显示在第二页

这是我的UsersController(请记住,用户有很多评论):

以下是我的用户显示视图:

<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| %>
    
    
    

    因为您没有使用过滤数组

    ,所以当您使用
    @user.comments.each
    时,您似乎仍在迭代所有用户的评论,这就是为什么您仍在每个页面中看到所有评论的原因。
    <% @comments.each do |comment| %>