Question

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

  • items ?? I cant understand the documentation github will_paginate however i know that if its just I can easily replace Category.all with

    Category.paginate(:page => params[:page], :per_page => 20, :order => 'created_at DESC')

    please help??

  • Was it helpful?

    Solution

    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| %>
    
    Licensed under: CC-BY-SA with attribution
    Not affiliated with StackOverflow
    scroll top