0

Following this SO post, I am trying to render indented comments using the acts_as_tree rails plugin with no success.

I believe the problem lies with this method (which I don't understand):

def indented_render(num, *args)
  render(*args).gsub(/^/, "\t" * num)
end

What does this method substitute? My partial is as follows:

%div{:id => "comment_#{comment.id}"}
  = comment.body
  = render :partial => 'comments/comment', :collection => comment.children
  - unless comment.children.empty?
    = indented_render 1, :partial => 'comments/comment', :collection => comment.children

However, none of the lines are indented. What am I doing wrong? Is there a better way to render the comments?

Update: This is the generated html:

<h1>Listing comments</h1>
<table>
  <tr>
    <td>
        <div id='comment_1'>
          (152) Facebook version of you: 400 friends. Real version of you: 4 friends
        <div id='comment_2'>
          (0) Well played.
            <div id='comment_3'>
              (0) I used to. Then I got married.
                <div id='comment_4'>
                  (17) math is hard
                    <div id='comment_5'>
                      (1) What's a math?
                        <div id='comment_6'>
                          (1) This made coke come out my nose.
                            <div id='comment_7'>
                              (2) So maybe I wasn't the best with fractions.
                            </div>
                            <div id='comment_8'>
                              (1) That sounds terribly painful. Please accept my apologies. Isn't it supposed to be going in your nose, not out?
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
Community
  • 1
  • 1
David
  • 7,310
  • 6
  • 41
  • 63
  • No, there are no tabs at all. I've attached what my html looks like. – David May 08 '11 at 08:31
  • I think I figured it out. I had `thread->comments` (2 different models) and I wasn't sure how to show the entire thread history that way, so I made a dummy root comment and made it the parent of the thread's comments (`thread->root_comment->comments`). Is there is a better way to do this? – David May 09 '11 at 09:29

1 Answers1

2

I think the tabs are just to make the generated HTML a bit prettier. It looks like the resultant HTML is properly nested to produce a tree-ish structure, you just need some CSS. First of all, you probably want a class on the comment wrapper <div>s so change this:

%div{:id => "comment_#{comment.id}"}

to this:

%div{:id => "comment_#{comment.id}", :class => 'comment'}

And then, in some CSS somewhere, try this:

.comment {
    margin-left: 20px;
}

That should indent the nested <div>s to give you a start at a tree structure.

Looks like you're using HAML and my HAML isn't that great but hopefully the above is close enough to correct to get you something useful.

mu is too short
  • 426,620
  • 70
  • 833
  • 800