我想要一个包含下一篇和上一篇文章的下一篇和上一篇链接
在一个简单的博客应用程序中,某人可以阅读一篇文章并决定阅读下一篇文章,而无需再次浏览文章列表,但单击下一篇按钮,该人将转到该列表中的下一篇文章。这怎么办呢。我曾与 willpaginate 和 kaminari 合作过,它们都是很棒的分页插件。
因此,现在在显示操作中,我想要一个下一个和上一个链接,其中包含下一篇和上一篇文章,我如何执行
下面这个简单的代码示例来使用
def show
@article = Article.find(params[:id])
end
In a simple blog application where someone can read an article and decide to read the next article without going through the list of articles again but click the next button and it takes the person to the next article on this list. How can this be done. I have worked with willpaginate and kaminari and they are both great plugins for pagination.
So now in the show action i want to have a next and previous link that featches the next and previous article how do i do this
simple code sample below to work with
def show
@article = Article.find(params[:id])
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以向
Article
添加一个方法来获取下一篇和上一篇文章,具体取决于下一篇和上一篇的含义 - 即您希望文章的顺序。例如使用
created_at
用于定义订单假设在日期没有任何相同的创建,这可能有点过于简单,因此您可能也想检查 id。比如:
如果你只想按 id 排序,那么这就更简单了,因为 id 永远不可能相同:
You could add a method to
Article
to get the next and previous articles depending on what you mean by next and previous - i.e. what order you want the articles in.E.g. with
created_at
used to define the orderThat assumes that nothing has the same created at date which might be a bit too simplistic so you might want to check against the id too. Something like:
If you just want to order by id then it is even more trivial as the ids can never be the same:
您可以使用 Willpaginate 插件来解决您的问题。
https://github.com/mislav/will_paginate
You can use Willpaginate plugin which solves Your problem.
https://github.com/mislav/will_paginate