how to accomplish pagination in RoR

Hi Folks,

   I am just trying to get started up in RoR, I have done a simple application of add, edit, delete....

    I am now trying to accomplish pagination in RoR, I referred a few tutorials, however none of the examples that i tried from there, seemed to work error free..... I have heard that lots of things have deprecated in RoR, can someone please post me a detailed report of how i can setup a pager in RoR, i mean the most straightforward and simple way to do it...

    I am using ruby 1.8.6

   Looking forward to help from all u guys...

Thanks and Regards, Durgesh.

Try will_paginate, and you'll love it.

durgesh wrote:

Hi,

You can generate scaffolding for a model (ruby script/generate scaffold <modelname>) and examine the resulting code - it should contain support for pagination. Be careful so you don't tell it to overwrite any files you care about!

Cheers

previous versions of rails < 2.x the pegination was built in with the classic pegination. but with rails2.0 it removed from the rails core. You need to look at the classic peginatior which is a built in for rails 1.8.x. or you can use a plugin like will_peginator

We are using will_paginate in the Rails 2 version of our BuildingWebApps.com site. I like it a lot better than the classic version. This article, starting halfway down got us going pretty well:

-Christopher

i was trying to install will_paginate, however i do not have svn on my machine...i tried the following commands:   ruby script/plugin   install svn://errtheblog.com/svn/plugins/will_paginate

can anyone please tell me how i can install the will_paginate plugin without svn?

durgesh wrote:

i was trying to install will_paginate, however i do not have svn on my machine...i tried the following commands:   ruby script/plugin   install svn://errtheblog.com/svn/plugins/will_paginate

can anyone please tell me how i can install the will_paginate plugin without svn?

PS C:\Documents and Settings\CynicalRyan> gem search will_paginate -r

*** REMOTE GEMS ***

Updating metadata for 27 gems from http://gems.rubyforge.org/ ........................... complete will_paginate (2.2.1)

- -- Phillip Gawlowski Twitter: twitter.com/cynicalryan

~ - You know you've been hacking too long when... ...you start reading car license plates as machine code. JMP 451, HCF 919 (this one really shook me up), HLT 772 (Halt instruction...gotta reboot the truck again!), FOXXY (um, don't know that one...must be a floating point instruction).

i get the following error:

ERROR: while executing gem ... <Gem::RemoteFetcher::FetchError>    A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. -connect<2> <Errno::ETIMEDOUT>         getting size of RubyGems.org | your community gem host

ok i got the will_paginate gem to sucessfully installe: now when i try to use the pagination i get the following error in the browser:

  NoMethodError in PeopleController#index undefined method `paginate' for Person(id: integer, name: string, address: text):Class RAILS_ROOT: C:/ruby/contact

Application Trace | Framework Trace | Full Trace C:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/ base.rb:1532:in `method_missing' app/models/person.rb:7:in `search' app/controllers/people_controller.rb:5:in `index'

this is the pagination code in controller:   def index     @people = Person.search(params[:search], params[:page])   end

this is the code in the model i.e person.rb def self.search(search, page)   paginate :per_page => 5, :page => page,            :conditions => ['name like ?', "%#{search}%"],            :order => 'name' end

and this is the code in the index.html.erb:

<h1>Listing people</h1>

<table>   <tr>     <th>Name</th>     <th>Address</th>   </tr>

<% for person in @people %>   <tr>     <td><%=h person.name %></td>     <td><%=h person.address %></td>     <td><%= link_to 'Show', person %></td>     <td><%= link_to 'Edit', edit_person_path(person) %></td>     <td><%= link_to 'Destroy', person, :confirm => 'Are you sure?', :method => :delete %></td>   </tr> <% end %> </table>

<br />

<!-- products/index.rhtml --> <%= will_paginate @people %>

<%= link_to 'New person', new_person_path %>

  Guys, does anyone know what mistake i might have made?

durgesh wrote:

ok i got the will_paginate gem to sucessfully installe: now when i try to use the pagination i get the following error in the browser:

Don't you think that actively looking for will_paginate's documentation might help?

That approach turned up the following site:

Which, oddly enough, tells you how to use will_paginate.

- -- Phillip Gawlowski Twitter: twitter.com/cynicalryan

~ "I suppose if we couldn't laugh at things that don't make sense, we couldn't react to a lot of life." -Hobbes

friends, thanks a lot for all the help offererd so far.....however, i am getting the same error, even after following the instructions:

undefined method `paginate' for Person(id: integer, name: string, address: text):Class RAILS_ROOT: C:/ruby/contact

Application Trace | Framework Trace | Full Trace C:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/ base.rb:1532:in `method_missing' app/models/person.rb:7:in `search' app/controllers/people_controller.rb:5:in `index'

this is the pagination code in controller:   def index     @people = Person.search(params[:search], params[:page])   end

this is the code in the model i.e person.rb def self.search(search, page)   paginate :per_page => 5, :page => page,            :conditions => ['name like ?', "%#{search}%"],            :order => 'name' end

and this is the code in the index.html.erb:

<h1>Listing people</h1>

<table>   <tr>     <th>Name</th>     <th>Address</th>   </tr>

<% for person in @people %>   <tr>     <td><%=h person.name %></td>     <td><%=h person.address %></td>     <td><%= link_to 'Show', person %></td>     <td><%= link_to 'Edit', edit_person_path(person) %></td>     <td><%= link_to 'Destroy', person, :confirm => 'Are you sure?', :method => :delete %></td>   </tr> <% end %> </table>

<br />

<!-- products/index.rhtml --> <%= will_paginate @people %>

<%= link_to 'New person', new_person_path %>

  Guys, does anyone know what mistake i might have made? please do help

Did you add the require 'will_paginate' in your environment, like it says in the docs ?

Fred

oh buddies, really very thankful to all of u...the problem was coming becoz i did not restart the webrick server after pasting the lines into the config/environment.rb...after i did that....things worked!!

thanks once again to all of u....looking forward to your help in future too.... :slight_smile: