简体   繁体   中英

Rendering nested/threaded comments

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>

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM