【问题标题】:Rails creating unordered list in HAMLRails 在 HAML 中创建无序列表
【发布时间】:2018-01-18 14:01:43
【问题描述】:

如果这个问题不合适,我很抱歉,但我正在尝试在 HAML 中呈现无序列表,HTML 版本如下所示:

<% @movies.comments.each do |comment| %>
  <ul class="comments">
    <li><%= comment.text %></li>
  </ul>
<% end %>

我使用了 3 种不同的 html->haml 在线转换器并且都渲染了这个 haml。

- @movies.comments.each do |comment|
  %ul.comments
    %li= comment.text

但是输出是这样的(电影中有 3 个 cmets):

<ul class="comments">
  <li>asdasdasd</li>
</ul>
<ul class="comments">
  <li>asdasdasd</li>
</ul>
<ul class="comments">
  <li>asdasdasd</li>
</ul>

为什么这会创建 3 个单独的 ul 元素,而不是其中的 1 个和 3 个 li 元素?

【问题讨论】:

    标签: ruby-on-rails haml


    【解决方案1】:

    这与 HAML 无关。它做它做的事情是因为你创建了ul element inside 循环。您要做的是将root ul 节点放在顶部:

    %ul.comments
      - @movies.comments.each do |comment|
        %li= comment.text
    

    【讨论】:

    • 天哪,我太笨了... :( 感谢您发现这一点。
    猜你喜欢
    • 2014-07-30
    • 2012-06-17
    • 2013-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-24
    相关资源
    最近更新 更多