渲染嵌套/线程注释
在这个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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这些选项卡只是为了让生成的 HTML 更漂亮一些。看起来生成的 HTML 已正确嵌套以生成树状结构,您只需要一些 CSS。首先,您可能想要在注释包装器
上有一个类,因此将其更改
为:
然后,在某些 CSS 中的某个地方,尝试这样做:
这应该缩进嵌套的
让您从树结构开始。
看起来您正在使用 HAML,而我的 HAML 不是很好,但希望上面的内容足够接近,可以纠正,为您提供有用的东西。
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:to this:
And then, in some CSS somewhere, try this:
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.