0

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) %>
Community
  • 1
  • 1
user2759575
  • 553
  • 3
  • 25

1 Answers1

1

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
Community
  • 1
  • 1
Pavan
  • 33,316
  • 7
  • 50
  • 76
  • Awesome, that was it. Any chance you'd know how to make the link the comment title instead of 'next'. I tried @commentnext.next.title with no success. – user2759575 May 24 '14 at 21:04
  • @user2759575 Try like this `<%= link_to "#{@comment.title}", post_comment_path(@post, @commentnext.next) %>` – Pavan May 24 '14 at 21:07