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

发布于 2024-08-12 16:53:56 字数 166 浏览 6 评论 0原文

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

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

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

没有心的人 2024-08-19 16:53:57

我们可以通过使用“will_paginate-bootstrap”gem 轻松完成此操作。

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

    gem 'will_paginate-bootstrap'

    现在运行捆绑安装。

  2. 在控制器中,您将使用 @post = Post.all 来获取
    来自模型的内容。
    相反,使用此

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

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

  1. 在index.html.erb中
    在代码末尾,即在结束 body 标记之前
    添加这个,

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

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

We can do this with ease by using '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.all to get the
    contents from models.
    Instead use this

@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.

  1. In index.html.erb
    At the end of the code i.e before ending the body tag
    Add this,

<%= 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.

并安 2024-08-19 16:53:57

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

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

Gemfile:

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

区域控制器 -->索引

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

index.html.erb

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

模型文件:

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

Adding pagination to a Ruby on Rails app

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

Gemfile:

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

Areas controller --> index

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

index.html.erb

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

Model file:

def self.pagination_request(page)
    paginate :per_page => 10, :page => page
end
南薇 2024-08-19 16:53:57

或者,小,快&积极维护: Pagy

Or, small, swifd & actively maintained: Pagy

焚却相思 2024-08-19 16:53:57

你可以使用雷宝石。

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

You can use the kaminari gem.

Very ease to use and highly customization friendly.

扭转时空 2024-08-19 16:53:56

您应该使用 gem will_paginate

安装和使用非常简单: https://github.com/mislav/will_paginate/wiki/安装

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

You should use the gem will_paginate.

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

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

李不 2024-08-19 16:53:56

will_paginate 绝对是可行的方法,我只是想添加一个指向 railscasts will_paginate 视频向您展示如何使用它,因为有时这可能是学习基础知识的更简单的方法而不是阅读文档。另外,您还将了解分页作为 Rails 的一部分时如何完成的简要历史(它更慢且更麻烦)。旧的经典分页也已移出到它自己的插件中,但只有当您将 Rails 升级到将其删除的版本时,才建议您已经在使用它。

Railscasts 还有大量其他精彩视频,可能会对您有所帮助。例如,一旦您变得更高级,您可能想尝试 ajax 分页

will_paginate is definitely the way to go, I just thought I'd add a link to a railscasts will_paginate video showing 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.

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

长安忆 2024-08-19 16:53:56

查看 will_paginate Gem。

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

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

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

<%= will_paginate @posts %>

Check out the will_paginate Gem.

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

@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 %>
时光沙漏 2024-08-19 16:53:56

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

控制器:

  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

视图:

...
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é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'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:

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é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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文