views:

33

answers:

1

please help me with pagination of this method

my method is this one

controller

#
def show
  @topic = Topic.find(params[:id]) 
  @posts = @topic.posts.find(:all ,:order=> 'id')
end

views

#
%div{:style=>"margin: 0 auto;"}
  %table.sortable.style2{:cellpadding=>5}
    %thead
      %tr
        %td{:width => "25%",:align => "center",:style => "font-weight: bold;"}Posted By
        %td{:width => "75%",:style => "font-weight: bold;"}Comments
    %tbody  
      - for post in @posts 
        %tr
          %td{:align => "center"}
            &nbsp
            %div{:width=>"5px" , :style=>"border: 1px solid rgb(232, 232, 232);background-color: rgb(248, 248, 248);width: 60px; height:60px;" }
              - if post.posted_by.image 
                = image_tag(post.posted_by.image.public_filename(),:width => "60px", :height => "60px",:align=>"center")
            %div{:style => "font-weight: bold; font-style: italic;"}
              = post.posted_by ? post.posted_by.firstname : "<em>Unknown User</em>" 
              %br
            %div{:style => "font-style: italic;"}
              Posted on 
              = post.created_at.strftime('%d of %B %Y ')
              = post.created_at.strftime('at %H:%M')  
          %td{:valign => "top"}
            =post.body
A: 

Pagination is really simply with mislav's will_paginate. Read the installation instructions, and the example in the README file. The example covers almost exactly what you're aiming at.

For your scenario, you want to start by editing your Post model class. Specify the amount of posts to view per page:

class Post < ActiveRecord::Base
  cattr_reader :per_page
  @@per_page = 10
end

You can use the paginate method on an association too, so this will work:

@posts = @topic.posts.paginate :order => 'id'

Then, in your view, where you want to show the page links, simply do:

= will_paginate @posts
Shtééf