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 does not need to be explicitly loaded in the rails program, because the init under the plug-in directory will be 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.

Add the following code to modify the index method of the controller:

# 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.