我想要一个包含下一篇和上一篇文章的下一篇和上一篇链接

发布于 2025-01-08 00:28:11 字数 285 浏览 0 评论 0原文

在一个简单的博客应用程序中,某人可以阅读一篇文章并决定阅读下一篇文章,而无需再次浏览文章列表,但单击下一篇按钮,该人将转到该列表中的下一篇文章。这怎么办呢。我曾与 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 技术交流群。

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

发布评论

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

评论(2

御弟哥哥 2025-01-15 00:28:11

您可以向 Article 添加一个方法来获取下一篇和上一篇文章,具体取决于下一篇和上一篇的含义 - 即您希望文章的顺序。

例如使用 created_at用于定义订单

def next
  self.class.find(
    :first, 
    :conditions => ["created_at > ?", self.created_at], 
    :order => 'created_at, id')
end

def previous
  self.class.find(
    :first, 
    :conditions => ["created_at < ?, self.created_at],
    :order => 'created_at desc, id desc')

假设在日期没有任何相同的创建,这可能有点过于简单,因此您可能也想检查 id。比如:

    :conditions => ["created_at > ? or (created_at = ? and id > ?)", self.created_at, self.created_at, self.id]

如果你只想按 id 排序,那么这就更简单了,因为 id 永远不可能相同:

def next
  self.class.find(:first,
    :conditions => ['id > ?', self.id],
    :order => 'id')
end

def previous
  self.class.find(:first,
    :conditions => ['id < ?', self.id],
    :order => 'id desc')
end

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 order

def next
  self.class.find(
    :first, 
    :conditions => ["created_at > ?", self.created_at], 
    :order => 'created_at, id')
end

def previous
  self.class.find(
    :first, 
    :conditions => ["created_at < ?, self.created_at],
    :order => 'created_at desc, id desc')

That 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:

    :conditions => ["created_at > ? or (created_at = ? and id > ?)", self.created_at, self.created_at, self.id]

If you just want to order by id then it is even more trivial as the ids can never be the same:

def next
  self.class.find(:first,
    :conditions => ['id > ?', self.id],
    :order => 'id')
end

def previous
  self.class.find(:first,
    :conditions => ['id < ?', self.id],
    :order => 'id desc')
end
ゝ偶尔ゞ 2025-01-15 00:28:11

您可以使用 Willpaginate 插件来解决您的问题。
https://github.com/mislav/will_paginate

You can use Willpaginate plugin which solves Your problem.
https://github.com/mislav/will_paginate

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