【问题标题】:Bootstrap grid system formatting photos in Rails在 Rails 中引导网格系统格式化照片
【发布时间】:2014-06-19 11:47:46
【问题描述】:

我正在使用引导程序。

我有一个包含许多产品的产品网站。我需要为“店面”以网格格式显示我的产品。每个产品都有一张照片和一些信息。

我看到了 thisthis 的帖子,但它们不是我想要的。

我正在尝试使用 product.each 迭代每个产品,同时尝试使用引导程序的网格系统。

随着时间的推移,我需要添加其他产品,因此需要每个产品从一排流到下一排。 我没有在 bootstrap 之外使用任何额外的 CSS 样式。

到目前为止,我似乎每个产品都在下降到下一行并且每个产品都在它自己的列中,但是我尝试调整每个产品所在的图像或 div 或 col 的大小,一切都变得不对齐.

这是我的 html.erb 文件:

<div class="row">
 <% @products.each do |product| %>
  <div class="col-md-3">
   <div id="storeimages">
    <%= image_tag(product.image_url, class: "img-responsive") %>
    <h3><%= product.title %></h3>
    <div><%= sanitize(product.description) %></div>
    <div><%= number_to_currency(product.price) %></div> 
    <%= button_to 'Add to Cart', line_items_path(product_id: product), 
         class: 'btn btn-info btn-sm', remote: true %>
   </div>
  </div>
 <% end %>
</div>

【问题讨论】:

    标签: html css ruby-on-rails twitter-bootstrap twitter-bootstrap-3


    【解决方案1】:

    假设你想要 4 列的行,你可以这样做:

    <% @products.in_groups_of(4, false).each do |group| %>
      <div class='row'>
        <% group.each do |product| %>
          <div class='col-md-3'>
            <%= image_tag(product.image_url, class: "img-responsive") %>
            <h3><%= product.title %></h3>
            <div><%= sanitize(product.description) %></div>
            <div><%= number_to_currency(product.price) %></div> 
            <%= button_to 'Add to Cart', line_items_path(product_id: product), class: 'btn btn-info btn-sm', remote: true %>
          </div>
        <% end %>
      </div>
    <% end %>
    

    这会将您的数据集分成 4 个项目的组,然后为每个组输出一个行 div。然后在每一行中,它在自己的列 div 中输出组的每个成员。

    传递给in_groups_offalse 确保您不会尝试为少于4 个项目的行输出列。如果您的产品总数不能被 4 整除,这将发生在您的最后一行,因为默认情况下,该函数将使用 nil 填充数组。

    感谢@vermin 提供fill_with 提示!

    【讨论】:

    • 您可以使用 :fill_with 选项来避免执行 if 产品:in_groups_of(4, false)
    • 谢谢@vermin - 我已经更新了我的答案来涵盖这个。
    • 非常简单地解决了我也在努力解决的问题。
    • 如何根据打开它的设备的大小响应地更改 4(方法 in_groups_of 中每组的数量)?
    • 如果您想要响应式布局,最好使用 flexbox 或其他 CSS 解决方案。如果您希望它响应屏幕尺寸,这不是您可以从服务器端生成的东西。
    【解决方案2】:

    我还添加了以下 css。

    对于 div.col-md-3:

    #productdescription { 
     height: 375px;
     width: 200px;
    }
    

    对于图片:

       #img {
         height: 200px;
         width: 180px;
         overflow: hidden;
        }
    

    【讨论】:

      猜你喜欢
      • 2016-03-11
      • 1970-01-01
      • 1970-01-01
      • 2014-12-23
      • 2016-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多