Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 链接到下一条注释的未定义方法_Ruby On Rails_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails 链接到下一条注释的未定义方法

Ruby on rails 链接到下一条注释的未定义方法,ruby-on-rails,ruby-on-rails-4,Ruby On Rails,Ruby On Rails 4,作为指导,我试图在我的评论显示页面上创建指向下一条评论的链接。然而,我得到了这个错误: undefined method `next' for #<Comment:0x00000103d59db0> 在我的帖子模型中,我有以下内容: def next post.comments.where("id > ?", id).order("id ASC").first end 我的意见: @post = Post.find(params[:post_id]) @commen

作为指导,我试图在我的评论显示页面上创建指向下一条评论的链接。然而,我得到了这个错误:

undefined method `next' for #<Comment:0x00000103d59db0>
在我的帖子模型中,我有以下内容:

 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) %>

从您所附的中,我猜您将
方法
放在了
错误的模型
中。它应该放在
注释
模型中

尝试将其放在评论模型中,而不是Post模型中

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

太棒了,就是这样。你有没有可能知道如何将链接改为评论标题而不是“下一步”。我尝试了@commentnext.next.title,但没有成功。@user2759575像这样尝试
#comment.rb
def next
   post.comments.where("id > ?", id).order("id ASC").first
end