0

The following problem has been fixed, I added an 's' to the end of comments, when it should just be comment, but a new problem has arisen at the bottom of this post.

I am trying to get threaded comments for my already existing comments system by following the answer to this man's question: Reddit-style nested/threaded/indented comments for Rails? When I try to access my article show page, I get the following error: undefined method `children' for # Here's the view from my article show page that is problematic:

<div id="comment <%= comment.id %>">
  <%= comment.title %>
  | <%= link_to "Edit Comment", edit_article_comment_path(@commentable, comment) %>
  | <%= link_to 'Delete Comment', [@commentable, comment], :confirm => "Are you sure?", :method => :delete %><br />
  <%= comment.content %><br />
  **<%= render :partial => 'comments/comment', :collection => @comments.children %>**
  <%= comment.user.name %><br /><br />
</div>

It's the render portion that is having a problem. If I take out the children part from the comments, I get this in my console: The recursion never stops. I get this: Rendered comments/_form.html.erb (14.9ms) Rendered comments/_comment.html.erb (2.0ms) Rendered comments/_comment.html.erb (7.3ms) Rendered comments/_comment.html.erb (12.3ms) Rendered comments/_comment.html.erb (17.9ms) Rendered comments/_comment.html.erb (22.3ms) Rendered comments/_comment.html.erb (26.8ms) Rendered comments/_comment.html.erb (31.6ms) Rendered comments/_comment.html.erb (36.2ms) .... I do not know why it keeps rendering. None of the comments have children, so it should stop. Here's the 'show' part of the article controller:

def show @article = Article.find(params[:id]) @commentable = Article.find(params[:id]) @comments = @commentable.comments.paginate(:page => params[:page]) @comment = Comment.new @title = @article.title end

My only explanation is that maybe there aren't any children comments, so will_paginate does not know what to do with it, so it throws an error at me.


New Problem, I am having problem with routing for comments. I am using polymorphic associations for my comments so they can be used for different models(articles, profiles, pictures, etc.), but I do not know how to create the routing paths in my view. Here is what I have now:

<div id="comment <%= comment.id %>">
  <%= comment.title %>
  | <%= link_to "Permalink", article_comment_path(@commentable, comment) %>
  | <%= link_to "Reply", new_article_comment_path(@commentable, @comment_child) %>
  | <%= link_to "Edit Comment", edit_article_comment_path(@commentable, comment) %>
  | <%= link_to 'Delete Comment', [@commentable, comment], :confirm => "Are you sure?", :method => :delete %><br />
  <%= comment.content %><br />
  <%= comment.user.name %><br /><br />
  <%= render :partial => 'comments/comment', :collection => @comment.children %>
</div>

There is an abundant number of pathing errors that won't work for other models. I am using: article_comment_path, new_article_comment_path, and edit_article_comment_path. By the way, this is inside of a for each loop, which loops over an array of comments. I want something like "commentable_comment_path" or "new_commentable_comment_path", so I can use it for my other models.

One more quick question, will this fix my "reply" link? I am not sure if I am doing that right. I want the reply link to create a comment within a comment.

Thank you so much.

Community
  • 1
  • 1
Kelp
  • 1,268
  • 5
  • 18
  • 32

1 Answers1

1

parent_id as you suggest is an integer reference to an object. To get the children for an object a you would look up all objects that have a parent_id of a.id.

As for the problem with the rendering: @comments is an array of comment objects - you have essentially asked for a page of comments by using paginate. I guess you want to render the comments themselves in which case :collection => @comments will do the job. This just means to use the partial for each item in the collection. The partial will then take care of rendering the children of each comment as per the linked answer.

Shadwell
  • 34,314
  • 14
  • 94
  • 99
  • edit: oops, did not know that enter posted a reply. The recursion never stops. I get this: Rendered comments/_form.html.erb (14.9ms) Rendered comments/_comment.html.erb (2.0ms) Rendered comments/_comment.html.erb (7.3ms) Rendered comments/_comment.html.erb (12.3ms) Rendered comments/_comment.html.erb (17.9ms) Rendered comments/_comment.html.erb (22.3ms) Rendered comments/_comment.html.erb (26.8ms) Rendered comments/_comment.html.erb (31.6ms) Rendered comments/_comment.html.erb (36.2ms) .... I do not know why it keeps rendering. None of the comments have children, so it should stop. – Kelp Sep 08 '10 at 23:24
  • Ah, have you replaced `:collection => comments.children` in the partial with the code in my answer? I meant the code in your top level view. To be honest I'm pretty sure it should be `comment.children` in the partial too instead of `comments.children`. – Shadwell Sep 08 '10 at 23:43
  • Haha, yeah it was a silly misplacement of the 's' in comments. It should only be 'comment'. I forgot, it was in a "for each comment in comments" loop. Thanks. Also, I have another problem, I am going to edit the main post with information about it. – Kelp Sep 09 '10 at 00:54
  • You'd probably be better off asking a new question and referring back to this one rather than tacking another question on. – Shadwell Sep 09 '10 at 08:08