【问题标题】:Loop showing unwanted divs when going through categories遍历类别时循环显示不需要的 div
【发布时间】:2016-06-10 11:46:18
【问题描述】:

我正在我的views/pages/index.html.erb 中处理这个循环

<div class="container-fluid">
  <div class="row">
    <% @products.each do |category, products| %>
      <% products.each_with_index do |product, index| %>
        <div class="col-lg-4 col-sm-6 col-xs-12 center-block">
          <% if index == 0 %>
            <%= image_tag product.image.url(:medium), class: "img-responsive" %>
            <div class="caption">
              <p><%= product.category.name %></p>
            </div> 
          <% end %>
        </div> 
      <% end %>
    <% end %>
  </div>
</div>

它工作得很好,并且完成了它应该做的事情,即遍历每个类别并呈现添加到每个类别的最后一张图片。

但问题是当我添加产品并将它们分配到views/products/new.html.erb 中的类别时,上面的循环似乎在行中添加了额外的空div's。我不明白为什么会这样......谁能给我一个提示为什么会这样?

这是它在谷歌开发工具中的显示截图

如您所见,该行中有四个空的不需要的 div。

【问题讨论】:

  • 我猜 条件应该在 div 之上,而不是在它之下,不是吗?
  • ehmmm....是的,这似乎是问题所在:)

标签: ruby-on-rails ruby twitter-bootstrap loops ruby-on-rails-4


【解决方案1】:

我不能保证,但我认为if 条件正在造成问题。

首先通过删除&lt;% if index == 0 %&gt;条件进行检查,如果它工作正常,对其进行必要的更改。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-26
    • 1970-01-01
    • 2012-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多