Reputation: 63
I have Categories view which list some items based on which categories the belong
now i want to paginate items on that category
So, in category controllerI have
<code>
def show
@category = Category.find(params[:id])
end
</code>
and in 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>
How to paginate those
Category.paginate(:page => params[:page], :per_page => 20, :order => 'created_at DESC')
please help??
Upvotes: 0
Views: 24
Reputation: 23949
Try:
def show
@category = Category.find(params[:id])
@photos = @category.photos.order('created_at DESC')
.paginate(page: params[:page], per_page: 20)
end
and in your view, change:
<% @category.photos.each do |photo| %>
into:
<% @photos.each do |photo| %>
Upvotes: 3