未定义的方法 `prev' 为

undefined method `prev' for

_表格

<%= link_to 'Previous Post', blog_path(@post).previous if @post.previous %>
<%= link_to 'Next Post', blog_path(@post).next if @post.next %>

伙计们,我在这里很困惑。 next 有效,但 previous 给我错误 undefined method 'previous' for "/blog/4":String

post.rb

  def next
    Post.where("id > ?", id).first
  end

  def previous
    Post.where("id < ?", id).last
  end

如果我将单词 previous 切换为 last,则错误消失,但不是链接到前一个 post,而是链接到当前的 post。

您在 blog_path 方法上调用 previousnext,returns 只是一个普通的 String.

您需要确保在 @post:

上调用它
blog_path(@post.previous)