user2759575
user2759575

Reputation: 553

Undefined method with link to next comment

Using this post as guidance I am trying to create a link to the next comment on my comments show page. I am getting this error however:

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

In my routes comments belong to posts:

resources :posts do
  resources :comments
end

In my posts model I have this:

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

My comments controller:

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

and then in my comments show view I have the link:

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

Upvotes: 0

Views: 51

Answers (1)

Pavan
Pavan

Reputation: 33552

From the Source you attached,I guess you put the method in wrong model.It should be in Comment model

Try putting this in the Comment model instead of Post model

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

Upvotes: 1

Related Questions