Ruby will_paginate usage

Source: Internet
Author: User

The new version of will_paginate has abandoned this practice and switched to the Gem method. This article builds an application named foobar to understand the usage of will_paginate.

====================================

C: \> rails foobar-D MySQL

C: \> Cd foobar

C: \ foobar> Ruby script/generate scaffold article title: String content: Text

In this case, set the database (C: \ foobar \ config \ database. yml) and make sure that the relevant database has been created.

C: \ foobar> rake DB: migrate

C: \ foobar> Ruby script/Server

Now, you can browse http: // localhost: 3000/articles and add some data (such as 10) to prepare for paging.

====================================

The old version of will_paginate in the form of plugin is not used in railsProgramIt is because the init under the plug-in directory is automatically executed when rails is started. RB file, and the new version of will_pagiante adopts the gem form, there is no such mechanism, so you need to explicitly load in the rails program:

# C: \ foobar \ config \ environment. Rb
Rails: initializer. Run do | config |
Config. Gem 'mislav-will_paginate',: version => '~> 2.3.2 ',: Lib => 'will _ paginate ',
: Source => 'HTTP: // gems.github.com'
End

Restart webrick and run the following command to install the corresponding GEM:

C: \ foobar> rake gems: Install

The installation is complete.

Modify the index method of the controller.Code:

# C: \ foobar \ app \ controllers \ articles_controller.rb
@ Articles = article. paginate: page => Params [: Page],: per_page => 2

Modify the corresponding template file and add code similar to the following:

# C: \ foobar \ app \ views \ articles \ index.html. ERB
<% = Will_paginate @ articles %>

Restart webrick again, And Then browse to view the page effect.

====================================

Will_paginate latest document: http://github.com/mislav/will_paginate/wikis

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.