未定义的方法,链接到下一条评论

时间:2014-05-24 19:56:35

标签: ruby-on-rails ruby-on-rails-4

使用this post作为指导我正在尝试在我的评论显示页面上创建指向下一条评论的链接。我收到了这个错误:

undefined method `next' for #<Comment:0x00000103d59db0>

在我的路线中,评论属于帖子:

resources :posts do
  resources :comments
end

在我的帖子模型中,我有这个:

 def next
   post.comments.where("id > ?", id).order("id ASC").first
end

我的评论控制器:

@post = Post.find(params[:post_id])
@comment = Comment.find params[:id]
@commentnext = @post.comments.find(params[:id])

然后在我的评论显示视图中我有链接:

<%= link_to "Next Comment", post_comment_path(@post, @commentnext.next) %>

1 个答案:

答案 0 :(得分:1)

从您附加的 Source ,我猜您将method放在 wrong model 。它应该在 Comment 模型

尝试将其放入评论模型而不是帖子模型

#comment.rb
def next
   post.comments.where("id > ?", id).order("id ASC").first
end