Ruby practice-will_paginate implement Paging

Source: Internet
Author: User

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:

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.