在 Ruby on Rails 中向 index.html.erb 添加分页 - 简单问题
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我们可以通过使用“will_paginate-bootstrap”gem 轻松完成此操作。
要继续,首先在 gem 文件中添加一行,
gem 'will_paginate-bootstrap'
。现在运行捆绑安装。
在控制器中,您将使用
@post = Post.all
来获取来自模型的内容。
相反,使用此
@post = Post.paginate(:page=>params[:page],per_page:5)
在上面的行 per_page 字段中指定您需要多少条记录
在一个页面中。
在代码末尾,即在结束 body 标记之前
添加这个,
<%= will_paginate @post,renderer: BootstrapPagination::Rails %>
注意:我认为 Post 作为模态,post 作为在控制器中声明的变量。
只需使用您的变量即可。
We can do this with ease by using 'will_paginate-bootstrap' gem.
To continue firstly you add a line to the gem file as,
gem 'will_paginate-bootstrap'
.Now run bundle install.
In the controller, you will be using like
@post = Post.all
to get thecontents 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.
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.
向 Ruby on Rails 应用程序添加分页
要向 Ruby on Rails 应用程序添加分页,您必须修改以下文件:
Gemfile:
区域控制器 -->索引
index.html.erb
模型文件:
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:
Areas controller --> index
index.html.erb
Model file:
或者,小,快&积极维护: Pagy
Or, small, swifd & actively maintained: Pagy
你可以使用雷宝石。
非常易于使用且高度定制友好。
You can use the kaminari gem.
Very ease to use and highly customization friendly.
您应该使用 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
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
查看 will_paginate Gem。
在 ActiveRecord 模型上进行分页非常容易:
在视图中,可以使用单个视图助手呈现页面链接:
Check out the will_paginate Gem.
It’s very easy to do pagination on ActiveRecord models:
In the view, page links can be rendered with a single view helper:
我在使用“will_paginate”时遇到问题,安装 GEM 然后卸载......一个真正的问题!所以我决定在RoR上编写分页程序,这并不困难,所以我想分享我所做的:
控制器:
视图:
我希望这对某人有帮助!
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:
View:
I hope this helps to someone!