渲染嵌套/线程注释

发布于 2024-11-05 19:13:03 字数 1952 浏览 0 评论 0原文

在这个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

随波逐流 2024-11-12 19:13:03

我认为这些选项卡只是为了让生成的 HTML 更漂亮一些。看起来生成的 HTML 已正确嵌套以生成树状结构,您只需要一些 CSS。首先,您可能想要在注释包装器

上有一个类,因此将其更改

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

为:

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

然后,在某些 CSS 中的某个地方,尝试这样做:

.comment {
    margin-left: 20px;
}

这应该缩进嵌套的

让您从树结构开始。

看起来您正在使用 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:

%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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文