简体   繁体   English

在没有will_paginate gem的Ruby中实现分页

[英]Implementing paginate in Ruby without will_paginate gem

I am approximately one month into an online Ruby on Rails course and am currently tasked with implementing pagination without the help of the will_paginate gem. 我大约有一个月的时间在线上Ruby on Rails在线课程,目前的任务是在没有will_paginate gem的帮助下实现分页。 The site I am building displays a list of topics, which each contain a number of posts. 我正在构建的网站显示一个主题列表,每个主题包含许多帖子。 The goal is to display 10 topics, or posts, per page. 目标是每页显示10个主题或帖子。

Here is what I have so far: 这是我到目前为止的内容:

I have created a Paginate module from which the Post and Topic models can call: 我创建了一个Paginate模块,Post和Topic模型可以从中调用:

module Paginate
  def paginate(args = {})

    self.limit(10).offset(args[:page] * 10)

  end
end

This is what I am calling in my Topics Controller: 这是我在主题控制器中调用的内容:

 def index
    params[:page] ||= 0  
    @topics = Topic.paginate(page: params[:page].to_i)
    authorize @topics
 end

I know this will only display the first 10 topics, I would have to manually enter a query string into the url to display each set of 10 (/topics?page=1 for instance). 我知道这只会显示前10个主题,我将不得不在url中手动输入查询字符串以显示每10个主题(例如,/ topics?page = 1)。

Does anyone have any suggestions for a view helper method that could clean this up? 有谁对可以解决此问题的视图帮助方法有任何建议? I am unsure how I could write something to generate an html link to each set of 10. 我不确定如何写一些东西来生成到每套10的html链接。

Thank you, 谢谢,

Matt 马特

The main thing that's missing from your code so far is that the paginate method needs to somehow return, along with the array of results, the total number of records , ie the number you would get back if you did that search without paginating. 到目前为止,您的代码中缺少的主要内容是,分页方法需要以某种方式返回结果的总数以及记录的总数 ,即返回的数目,即如果您不进行分页而进行的搜索将获得的记录数。 The reason you need this is so you know how to construct the helper, which will have (in the simplest case) one link per page. 之所以需要它,是因为您知道如何构造帮助器,该帮助器(在最简单的情况下)每页只有一个链接。

eg if your page size is 10, and there are 46 records, that will be five pages in total. 例如,如果您的页面大小为10,并且有46条记录,则总共有5页。 So your helper will have links to page 1, page 2, page 3, page 4, page 5, and also maybe "Previous" and "Next", or something along those lines. 因此,您的助手将具有指向第1页,第2页,第3页,第4页,第5页的链接,并且可能还有“上一页”和“下一页”等链接。

WillPaginate communicates this by returning a new class of object, that extends Array (or extends ActiveRecord::Collection or whatever you normally get back from Topic.all), and adds in some extra methods like total_entries which tell you the extra info. WillPaginate通过返回一个新的对象类来传达此信息,该对象类扩展了Array(或扩展了ActiveRecord :: Collection或通常从Topic.all返回的任何内容),并添加了一些额外的方法,例如total_entries ,它们告诉您额外的信息。

The advantage of this is that because paginate returns the same sort of thing that a normal search would, with a bit of extra info attached, you can subsititute a paginated search for a non-paginated search without having to change all the rest of your code: it's like a normal search with some extra "stuff". 这样做的好处是,由于PAGINATE返回相同的排序 ,一个正常的搜索会,有一些额外的信息连接,您可以subsititute对于非分页搜索分页搜索,而无需事情来改变你的代码中所有的休息:这就像带有一些“东西”的普通搜索。

The only bit of extra info you really need is the "total_entries" figure: you know which page you asked for and how many results per page, so you can calculate everything else you need for the pagination helper from these three numbers. 您真正需要的唯一额外信息是“ total_entries”图:您知道所要的页面以及每页有多少结果,因此您可以从这三个数字中计算出分页助手所需的所有其他信息。

Note i'm not giving you the code here, just a pointer in the right direction. 注意,我在这里没有给您提供代码,只是向正确方向提供了一个指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM