Ruby on Rails - 在一个视图中对两个列表进行分页的正确方法
Ruby on Rails - Correct way of paginating two lists in one view
所以我想在一个页面上使用两次will_paginate插件。我的问题是,当我在一个分页(@articles 分页)上单击下一步时,它也会转到我的另一个分页(@comments 分页)的下一页。
<div class="row">
<div class="span6">
<h3>Posted Articles (<%= @articles.count %>)</h3>
<ol class="user_articles">
<% @articles.each do |article| %>
<li>
<span class="content"><%= link_to article.title, article.content %></span>
<span class="timestamp">
Posted <%= time_ago_in_words(article.created_at) %> ago to <%= article.article_type %>.
</span>
<div>
<% if current_user?(article.user) %>
<%= link_to "delete", article, method: :delete,
data: { confirm: "You sure?" },
title: article.title %>
<% end %>
</div>
</li>
<% end %>
</ol>
<%= will_paginate @articles %>
</div>
<div class="span6">
<h3>Comments (<%= @comments.count %>)</h3>
<ol class="user_comments">
<% @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 %>
</div>
</div>
我知道你可以指定一个 :param_name 选项来告诉 will_paginate 用于页码的参数名称,但我不知道是什么 :param_name 我应该使用它才能工作。任何帮助将不胜感激!
编辑:
这是我的视图用户控制器:
def show
@user = User.find(params[:id])
@comments = @user.comments.paginate(page: params[:page], :per_page => 20)
@articles = @user.articles.paginate(page: params[:page], :per_page => 20)
end
您的 :param_name
应该类似于 :articles_page
和 :comments_page
,在每个分页语句中指定。
所以我想在一个页面上使用两次will_paginate插件。我的问题是,当我在一个分页(@articles 分页)上单击下一步时,它也会转到我的另一个分页(@comments 分页)的下一页。
<div class="row">
<div class="span6">
<h3>Posted Articles (<%= @articles.count %>)</h3>
<ol class="user_articles">
<% @articles.each do |article| %>
<li>
<span class="content"><%= link_to article.title, article.content %></span>
<span class="timestamp">
Posted <%= time_ago_in_words(article.created_at) %> ago to <%= article.article_type %>.
</span>
<div>
<% if current_user?(article.user) %>
<%= link_to "delete", article, method: :delete,
data: { confirm: "You sure?" },
title: article.title %>
<% end %>
</div>
</li>
<% end %>
</ol>
<%= will_paginate @articles %>
</div>
<div class="span6">
<h3>Comments (<%= @comments.count %>)</h3>
<ol class="user_comments">
<% @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 %>
</div>
</div>
我知道你可以指定一个 :param_name 选项来告诉 will_paginate 用于页码的参数名称,但我不知道是什么 :param_name 我应该使用它才能工作。任何帮助将不胜感激!
编辑:
这是我的视图用户控制器:
def show
@user = User.find(params[:id])
@comments = @user.comments.paginate(page: params[:page], :per_page => 20)
@articles = @user.articles.paginate(page: params[:page], :per_page => 20)
end
您的 :param_name
应该类似于 :articles_page
和 :comments_page
,在每个分页语句中指定。