ROR汇集---will_paginate

WillPaginate是一款用于Ruby on Rails应用的分页插件,它简化了数据库记录的分页显示过程。通过简单的配置,开发者可以在控制器中轻松实现分页功能,并在视图中快速生成分页链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转自:http://github.com/mislav/will_paginate/tree/master

WillPaginate

Pagination is just limiting the number of records displayed. Why should you let it get in your way while developing, then? This plugin makes magic happen. Did you ever want to be able to do just this on a model:

   

… and then render the page links with a single view helper? Well, now you can.

Some resources to get you started:

Example usage

Use a paginate finder in the controller:

   

Yeah, paginate works just like find — it just doesn’t fetch all the records. Don’t forget to tell it which page you want, or it will complain! Read more on WillPaginate::Finder::ClassMethods.

Render the posts in your view like you would normally do. When you need to render pagination, just stick this in:

   

You’re done. (You can find the option list at WillPaginate::ViewHelpers.)

How does it know how much items to fetch per page? It asks your model by calling its per_page class method. You can define it like this:


… or like this: 

… or don’t worry about it at all. WillPaginate defines it to be 30 by default. But you can always specify the count explicitly when calling paginate:

 

The paginate finder wraps the original finder and returns your resultset that now has some new properties. You can use the collection as you would with any ActiveRecord resultset. WillPaginate view helpers also need that object to be able to render pagination:

 

More detailed documentation:

  • WillPaginate::Finder::ClassMethods for pagination on your models;
  • WillPaginate::ViewHelpers for your views.

Authors and credits

Authors:Mislav Marohnić, PJ Hyett
Original announcement:errtheblog.com/post/929
Original PHP source:www.strangerstudios.com/sandbox/pagination/diggstyle.php

 

 

All these people helped making will_paginate what it is now with their code contributions or just simply awesome ideas:

Chris Wanstrath, Dr. Nic Williams, K. Adam Christensen, Mike Garey, Bence Golda, Matt Aimonetti, Charles Brian Quinn, Desi McAdam, James Coglan, Matijs van Zuijlen, Maria, Brendan Ribera, Todd Willey, Bryan Helmkamp, Jan Berkel, Lourens Naudé, Rick Olson, Russell Norris, Piotr Usewicz, Chris Eppstein, Denis Barushev, Ben Pickles.

Usable pagination in the UI

There are some CSS styles to get you started in the "examples/" directory. They are showcased online here.

More reading about pagination as design pattern:

Want to discuss, request features, ask questions? Join the Google group.

 

建议:

1、如果项目对分页的参数大多数要求一致的话。分页参数可以写进yml文件。

2、分页可以先查找,再调用paginate插件。如:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值