Search code examples
ruby-on-railsrubywill-paginate

Ruby on Rails: will_paginate is not working. Next page still showing all comments


So I have no idea what I'm doing wrong here, but my will_paginate method is not working as expected. The problem is that I want to set a per page limit, but all comments still show up in the first page, and when I click the next button, all comments still show up in the second page.

Here is my UsersController (keep in mind that users have many comments):

def show
@user = User.find(params[:id])
@comments = @user.comments.paginate(page: params[:page], :per_page => 20)
end

Here is my users show view:

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

The weird thing is that I have very similar code working correctly in my articles show view (articles also have many comments)... Any help would be greatly appreciated!


Solution

  • Your error is very simple :) This line :

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

    Should be replaced by :

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

    Because you were not using the filtered array