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

查看:20
本文介绍了项目的页面和 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 '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天全站免登陆