2011-05-07 102 views
0

在此之后SO post,我试图渲染使用acts_as_tree轨,但没有成功插件缩进意见。渲染嵌套/线程评论

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

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> 
+0

不,根本没有标签。我附上了我的html的样子。 – David 2011-05-08 08:31:20

+0

我想我明白了。我有'thread-> comments'(2个不同的模型),我不知道如何以这种方式显示整个线程历史记录,所以我做了一个虚拟根注释并将它作为线程注释的父项('thread-> root_comment-> comments')。有没有更好的方法来做到这一点? – David 2011-05-09 09:29:21

回答

2

我觉得选项卡只是为了让生成的HTML有点漂亮。它看起来像生成的HTML正确嵌套以产生树状结构,您只需要一些CSS。首先,你可能想在注释包装类<div>这么改变的:

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

这样:

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

,然后在一些CSS的地方,试试这个:

.comment { 
    margin-left: 20px; 
} 

这应该缩进嵌套<div> s到让你在树结构的开始。

看起来你正在使用HAML和我的HAML不是很大,但希望上面是足够接近正确,让你一些有用的东西。