在 Ruby on Rails 中向 index.html.erb 添加分页 - 简单问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1771308/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:58:53  来源:igfitidea点击:

Adding pagination to index.html.erb in Ruby on Rails - Easy Question

ruby-on-railsrubypaginationblogs

提问by bgadoci

I am new to rails so go easy. I have built my first blog and would like to set it up such that in the <% post.each do |post| %> render of the posts, I would like it to work such that it only displays 10 posts and then has a link to view the next 10.

我是 Rails 的新手,所以放轻松。我已经建立了我的第一个博客,并希望在 <% post.each do |post| 中设置它。%> 呈现帖子,我希望它能够工作,使其只显示 10 个帖子,然后有一个链接来查看接下来的 10 个帖子。

I am hoping this is an easy question. Let me know if you would like me to post any code.

我希望这是一个简单的问题。如果您希望我发布任何代码,请告诉我。

回答by mmrobins

will_paginateis definitely the way to go, I just thought I'd add a link to a railscasts will_paginate videoshowing you how to use it since sometimes that can be an easier way to learn the basics than reading documentation. Plus you'll learn a brief history on how pagination used to be done when it was an included part of Rails (it was slower and more cumbersome). The old classic pagination has been moved out into it's own plugin too, but it's only recommended if you were already using it when you upgraded Rails to a version where they took it out.

will_paginate绝对是要走的路,我只是想我会添加一个链接到railscasts will_paginate 视频,向您展示如何使用它,因为有时这比阅读文档更容易学习基础知识。此外,您还将了解分页作为 Rails 的一部分时过去如何完成的简要历史(它更慢且更麻烦)。旧的经典分页也已移出到它自己的插件中,但仅当您将 Rails 升级到他们删除它的版本时已经在使用它时才建议使用它。

Railscasts has a ton of other great videos that you might find helpful. For example, once you get more advanced you might want to try ajax pagination

Railscasts 有大量其他很棒的视频,您可能会觉得它们很有帮助。例如,一旦你变得更高级,你可能想尝试ajax 分页

回答by brainfck

You should use the gem will_paginate.

您应该使用 gem will_paginate

The installation and usage is really easy: https://github.com/mislav/will_paginate/wiki/installation

安装和使用真的很简单:https: //github.com/mislav/will_paginate/wiki/installation

Example usage: http://github.com/mislav/will_paginate

用法示例:http: //github.com/mislav/will_paginate

回答by Simone Carletti

Check out the will_paginateGem.

查看will_paginate宝石。

It's very easy to do pagination on ActiveRecord models:

在 ActiveRecord 模型上进行分页非常容易:

@posts = Post.paginate :page => params[:page], :order => 'created_at DESC'

In the view, page links can be rendered with a single view helper:

在视图中,可以使用单个视图助手呈现页面链接:

<%= will_paginate @posts %>

回答by Ivan Carrasco Quiroz

I've got problems using "will_paginate", installing the GEM then unistalling... a REAL PROBLEM! So I decide to program the pagination on RoR, it was not difficult so I wanted to share what I did:

我在使用“will_paginate”时遇到问题,安装 GEM 然后卸载……一个真正的问题!所以我决定在 RoR 上编写分页程序,这并不难,所以我想分享我所做的:

Controller:

控制器:

  def index
#how many register per page i want to show
    @b = 30 
#params via GET from URL
    a = params[:page].to_i
#first register per page
    a1 = params[:page].to_i * @b
#the query can be easy...
    @callcenters = Income.all(:joins => "LEFT JOIN radcheck ON radcheck.id = incomes.radcheck_id", :order => "created_at DESC",:limit => "#{a1},#{@b}", :select => "radcheck.username as clave,caller_id, created_at, value")
#I need to know how many pages somehow
     @registrostotales = Income.all(:joins => "LEFT JOIN radcheck ON radcheck.id = incomes.radcheck_id", :select => "radcheck.username as clave,caller_id, created_at, value").count

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @callcenters }
    end
  end

View:

看法:

...
Total de registros: <%= numero = @registrostotales %><br/><br/>
<!-- total pages -->
<% pages = @registrostotales / @b %>
<!-- the last page -->
<% if pages % @b > 0 %><% pages = pages + 1 %><% end %>

Paginas:
<% (0..(pages-1)).each do |i| %>
<!-- href or link_to, http://xxxxxxxx/yyyy?page=i -->
  <%= link_to i+1, :action => "index", :controller => "callcenters", :page => i %>
<% end %>


<!-- the view.. -->
<table>
  <tr>
    <th>#</th>
    <th>Tel&eacute;fono (ID)</th>
    <th>Zona</th>
  </tr>

<% @callcenters.each do |callcenter| %>
  <tr>
    <td><%= numero - params[:page].to_i * 30 %><% numero = numero  - 1 %></td>
    <td><%= callcenter.caller_id %></td>
    <td><%= Node.first(:conditions => {:calling_id => callcenter.caller_id}).description %></td>
  </tr>
<% end %>
</table>

I hope this helps to someone!

我希望这对某人有所帮助!

Best Regards, ivancarrascoq

最好的问候,伊万卡拉斯科克

回答by Chaithanya Krishna

We can do this with ease by using 'will_paginate-bootstrap' gem.

我们可以通过使用 'will_paginate-bootstrap' gem 轻松地做到这一点。

  1. To continue firstly you add a line to the gem file as,

    gem 'will_paginate-bootstrap'.

    Now run bundle install.

  2. In the controller, you will be using like @post = Post.allto get the contents from models. Instead use this

  1. 要继续,首先将一行添加到 gem 文件中,

    gem 'will_paginate-bootstrap'.

    现在运行捆绑安装。

  2. 在控制器中,您将使用 like@post = Post.all从模型中获取内容。而是使用这个

@post = Post.paginate(:page=>params[:page],per_page:5)

@post = Post.paginate(:page=>params[:page],per_page:5)

Here in the above line per_page field is to specify how many records you need in a page.

上面一行中的 per_page 字段是指定一个页面需要多少条记录。

  1. In index.html.erb At the end of the code i.e before ending the body tag Add this,
  1. 在 index.html.erb 的代码末尾即结束 body 标签之前添加这个,

<%= will_paginate @post,renderer: BootstrapPagination::Rails %>

<%= will_paginate @post,renderer: BootstrapPagination::Rails %>

Note: I thought Post as modal and post as variable declared in the controller. Just go with your variables.

注意:我认为 Post 是模态的,post 是控制器中声明的变量。只需使用您的变量即可。

回答by sivamani

Adding pagination to a Ruby on Rails app

向 Ruby on Rails 应用程序添加分页

To add pagination to your Ruby on Rails app, you have to modify the following files:

要将分页添加到您的 Ruby on Rails 应用程序,您必须修改以下文件:

Gemfile:

宝石档案:

gem 'will_paginate', '~> 3.1.0'
gem 'will_paginate-bootstrap'

Areas controller --> index

区域控制器 --> 索引

@areas = Area.pagination_request(params[:page])

index.html.erb

index.html.erb

<%= will_paginate @areas, renderer: BootstrapPagination::Rails %>

Model file:

模型文件:

def self.pagination_request(page)
    paginate :per_page => 10, :page => page
end

回答by Ashish Garg

You can use the kaminari gem.

您可以使用 kaminari gem。

Very ease to use and highly customization friendly.

非常易于使用和高度定制友好。

回答by lumpidu

Or, small, swifd & actively maintained: Pagy

或者,小,快速且积极维护:Pagy