我有类别视图,根据属于哪个类别列出一些项目
现在我想对该类别的项目进行分页
所以,在类别controllerI中有
<code>
def show
@category = Category.find(params[:id])
end
</code>
和views / categories / show.html.erb
<code>
<%= will_paginate %>
<ul>
<% @category.photos.each do |photo| %>
<li><%= link_to image_tag( photo.image_url(:thumb).to_s ), photo %></li>
<% end %>
</ul>
<%= will_paginate %>
</code>
如何对那些
轻松替换Category.all
Category.paginate(:page => params[:page], :per_page => 20, :order => 'created_at DESC')
请帮忙??
答案 0 :(得分:3)
尝试:
def show
@category = Category.find(params[:id])
@photos = @category.photos.order('created_at DESC')
.paginate(page: params[:page], per_page: 20)
end
并在您看来,更改:
<% @category.photos.each do |photo| %>
成:
<% @photos.each do |photo| %>