1
所以我不知道我在做什麼錯在這裏,但我的will_paginate方法不按預期工作。問題是我想設置每頁限制,但所有評論仍顯示在第一頁,當我點擊下一個按鈕時,所有評論仍顯示在第二頁。Ruby on Rails: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'時,仍然會遍歷所有用戶的評論,這就是爲什麼您仍然可以在每個頁面看到所有評論的原因。 – hbejgel 2015-02-23 17:23:48