项目的页面和will_paginate [英] Item's page and will_paginate

查看:57
本文介绍了项目的页面和will_paginate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些照片是通过will_paginate插件分割在连续的页面上的.当您打开照片然后使用链接返回所有照片时,您始终会返回首页(例如,该照片显示在第5页上,打开照片,单击链接以显示所有照片并期望您正在再次进入第5页,但您位于第1页).

I have some photos that are split on successive pages through will_paginate plugin. When you open a photo and then return to all photos using a link, you always return to the first page (e.g. the photo is displayed on page 5, you open the photo, click a link to show all photos and expect that you are on page 5 again, but you are on page 1).

现在,有什么方法可以获取照片所属的页码吗?

Now, is there any method to get the page number to which a photo belongs to?

我尝试传递GET参数,但这仅在用户不再执行任何其他操作(例如发表评论,编辑照片等)时才有效.

I tried to pass a GET parameter, but this only works if the user doesn't perform any more actions (e.g. post a comment, edit photo, ecc.).

推荐答案

page = (number_of_records_before_RECORD / number_of_records_per_page) + 1

换句话说.如果您的照片的ID为40,并且之前有25条记录(假设某些记录已被删除),每页20条记录:

In other words. If your photo has ID 40 and there are 25 records before (assuming some records have been deleted), with 20 records per page:

page = (25 / 20) + 1 = 2

您可以使用Model.count(:conditions => ['id < ?', record.id], :order => 'id')计算所选记录之前的记录数. 正确的查询取决于列出所有对象时对表应用的排序筛选器.

You can count the number of records before the selected record using Model.count(:conditions => ['id < ?', record.id], :order => 'id'). The right query depends on which sorting filter you apply to that table when listing all objects.

这篇关于项目的页面和will_paginate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆