Colin L. wrote in post #1075895:
On 13 September 2012 19:22, Kyle A. [email protected] wrote:
So for the convince of all I have posted my code on gist. I am having
issues using the ancestry gem to nest comments. the issue is that said
comment does not nest under the parent comment, instead it is added to
the end of the comments list.
I have a relationship of tasks to comments, where a task has many
comments and a comment belongs to task. Your help is appreciated.
My code is posted here: Help with nesting comments · GitHub
Have a look at the Rails Guide on Debugging. That will show you ways
of debugging your code. First home in on /exactly/ which section is
failing so that rather than saying “comment does not nest under the
parent comment, instead it is added to the end of the comments list”
you can point to a couple of lines of code and say what it is doing
wrong, so you can ask here again. In practice it is likely that
having homed in on the lines of code you will see yourself what the
problem is.
Colin
taking this into consideration and spending what seems like for ever
scouring the internet and reading articles, even going over the tutorial
I go thris from I have come to the conclusion that it is a few things.
One:
module CommentsHelper
def nested_comments(comments)
comments.map do |comment, sub_comments|
render(comment) + content_tag(:div, nested_comments(sub_comments),
:class => ‘nested_comments’)
end.join.html_safe
end
end
the code should display something like this when I rendered out
comments,
before it could do comments.map
{ #<TreeNode id: 100018, name: “Stinky”, ancestry: nil>
=> { #<TreeNode id: 100019, name: “Crunchy”, ancestry: “100018”>
=> { #<TreeNode id: 100020, name: “Squeeky”, ancestry:
“100018/100019”>
=> {}
}
}
}
except it doesn’t, I get
{ #<TreeNode id: 100018, name: “Stinky”, ancestry: nil>}
{ #<TreeNode id: 100019, name: “Crunchy”, ancestry: nil>}
{ #<TreeNode id: 100020, name: “Squeeky”, ancestry: nil>}
{}
So there’s something wrong with the helper even when its the same as the
example tutorial I was following
Two:
parent_id is apparently an attribute of comments, how ever in the
example I followed they did not have to assign it as a accessible
attribute, where as I do - else I get an error - plus there is no column
for parent_id - as there ins’t suppose to be one, just ancestry and an
index of ancestry - which I have tripple checked
How ever with all that said the code seems to work. so I kept moving on
- One of the other issues after playing with the code is the helper
method.
if I take this:
module CommentsHelper
def nested_comments(comments)
comments.map do |comment, sub_comments|
render(comment) + content_tag(:div, nested_comments(sub_comments),
:class => ‘nested_comments’)
end.join.html_safe
end
end
and replace the nested_comments(sub_comments) with ‘hello’ I get
[commnet]
hello
[comment]
hello
So with that new information. what else should I be looking for? I have
red all the documentation I can, poured over examples, read he out put a
thousand times. I have printed out comments, I have tried different
styles such as @tasks.comments - as comments belong to tasks. and still
I am lost.