使用渲染集合渲染树

发布于 2024-11-15 13:33:10 字数 1546 浏览 0 评论 0原文

我有一个有评论的网站。显示它们时,项目的第一个评论不应缩进,但后续的评论应缩进。

HTML 输出应类似于:

<ul>
  <li>review for item 1</li>
  <li>
    review for item 2
    <ul>
      <li>2nd review  for item 2</li>
      <li>3rd review for item 2</li>
    </ul>
  </li>
</ul>

我得到的输出如下所示:

<ul>
  <li>review for item 1</li>
  <li>
    review for item 2
  </li> <!-- wrong -->
  <li>  <!-- wrong -->
    <ul>
      <li>2nd review  for item 2</li>
      <li>3rd review for item 2</li>
    </ul>
  </li>
</ul>

_reviews.html.erb

<%= render :partial => "review_item", :collection => @reviews, :locals => { :reviews_local => @reviews } %>

_review_item.html.erb

<% if review_item_counter == 0 %>
<ul>
<% end %>

    <li>
    <% if @current_reviewable != review_item.id %>
       <% @current_reviewable = review_item.id %>
           <%= review_item.body %>

    <% else %>

        <% reviews_local.slice!(review_item_counter, reviews_local.size) %>
        <%= render :partial => "review_item", :collection => reviews_local, :locals => { :reviews_local => reviews_local } %>

    <% end %>
    </li>

<% if review_item_counter == reviews_local.size - 1 %>
</ul>
<% end %>

I have a site with reviews. When displaying them, the first review for an item should not be indented, but subsequent ones should be.

The HTML output should look something like:

<ul>
  <li>review for item 1</li>
  <li>
    review for item 2
    <ul>
      <li>2nd review  for item 2</li>
      <li>3rd review for item 2</li>
    </ul>
  </li>
</ul>

The output I'm getting looks like this:

<ul>
  <li>review for item 1</li>
  <li>
    review for item 2
  </li> <!-- wrong -->
  <li>  <!-- wrong -->
    <ul>
      <li>2nd review  for item 2</li>
      <li>3rd review for item 2</li>
    </ul>
  </li>
</ul>

_reviews.html.erb

<%= render :partial => "review_item", :collection => @reviews, :locals => { :reviews_local => @reviews } %>

_review_item.html.erb

<% if review_item_counter == 0 %>
<ul>
<% end %>

    <li>
    <% if @current_reviewable != review_item.id %>
       <% @current_reviewable = review_item.id %>
           <%= review_item.body %>

    <% else %>

        <% reviews_local.slice!(review_item_counter, reviews_local.size) %>
        <%= render :partial => "review_item", :collection => reviews_local, :locals => { :reviews_local => reviews_local } %>

    <% end %>
    </li>

<% if review_item_counter == reviews_local.size - 1 %>
</ul>
<% end %>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文