渲染嵌套/线程注释

时间:2011-05-07 12:12:52

标签: html ruby ruby-on-rails-3 methods partial-views

SO post之后,我试图使用acts_as_tree rails插件渲染缩进的注释,但没有成功。

我认为问题在于这种方法(我不明白):

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

这种方法替代了什么?我的部分内容如下:

%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

但是,没有一行是缩进的。我究竟做错了什么?有没有更好的方式来呈现评论?

更新:这是生成的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>

1 个答案:

答案 0 :(得分:2)

我认为这些标签只是为了让生成的HTML更漂亮。它看起来像生成的HTML被正确嵌套以产生树形结构,你只需要一些CSS。首先,你可能想要一个关于评论包装器<div>的类,所以改变这个:

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

到此:

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

然后,在某个地方的CSS中,试试这个:

.comment {
    margin-left: 20px;
}

那应该缩进嵌套的<div>,以便从树结构开始。

看起来你正在使用HAML,而我的HAML并不是那么好,但希望上面的内容足够接近,以便为你提供有用的东西。