Development Environment:
OS: Windows XP
RUBY: ruby1.9.1
Rails: rails2.3.5
Will_paginate: will_paginate2.3.11
(Run the gem install mislav-will_paginate -- source http://gems.github.com in the command line)
IDE: rubymine2.0.1
DB: mysql5.0.9
This example uses the will_paginate plug-in to implement paging based on the previous example (Ruby practice-simple database operations ).
1. Install will_paginate
(Run the gem install mislav-will_paginate -- source http://gems.github.com in the command line)
Ii. Modify enviroment. Rb
Reference "will_paginate", in
Rails: initializer. Run do | config |
End
Then add require 'will _ paginate'. Otherwise, the error "method not found 'paginate'" will be reported during running '"
3. Modify product_controller.rb
The method for modifying the index is as follows:
Def index <br/> # @ products = product. all <br/> @ products = product. paginate: page => Params [: Page] | 1,: per_page => 2 <br/> respond_to do | format | <br/> format.html # index.html. ERB <br/> format. XML {render: XML =>@ products} <br/> end
# Note: 1 is the data displayed on page 3000 with http: // localhost: 1st/products; 2 is the number of records displayed on each page.
@ Product_pages = product. paginate: page => Params [: Page] | 1,: per_page => 2
Modify index.html. ERB
Add the following reference
<% = Will_paginate @ products,: prev_label => 'pre',: next_label => 'Next' %>
Running result: