【问题标题】:How do I get Ruby on Rails to display data as a Bootstrap grid?如何让 Ruby on Rails 将数据显示为 Bootstrap 网格?
【发布时间】:2017-09-13 12:33:26
【问题描述】:

我正在将 Bootstrap 4 专辑模板应用于我正在开发的 Ruby on Rails 网站。原始专辑模板如下所示:

但我的显示不是这样。它显示为一个列表,一个在另一个之下,类似于博客页面的外观。

我认为我需要进行必要更改的文件位于 app/views/products/index.hmtl.erb 中:

<div class="album text-muted">
  <div class="container">

    <div class="row">
      <div class="card">

        <%= render @products %>

          <% @products.each do |product| %>
           <%= product.image %>
          <% end %>
        <p class="card-text">
          <% @products.each do |product| %>
           <%= product.description %>
          <% end %>
        </p>
      </div>
    </div>

  </div> 
</div>


<%= paginate @products %>

<%= link_to 'New Product', new_product_path if logged_in?(:site_admin) %>

我没有遗漏原始模板中的任何 Bootstrap 类,所以我想知道是不是我的代码导致它无法正确呈现。

这里是 stylesheets/products.scss 文件:

// Place all the styles related to the products controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
// Custom bootstrap variables must be set or imported *before* bootstrap.
@import "bootstrap";
@import "font-awesome";

body {
  min-height: 75rem; /* Can be removed; just added for demo purposes */
}

.navbar {
  margin-bottom: 0;
}

.jumbotron {
  padding-top: 6rem;
  padding-bottom: 6rem;
  margin-bottom: 0;
  background-color: #fff;
}

.jumbotron p:last-child {
  margin-bottom: 0;
}

.jumbotron-heading {
  font-weight: 300;
}

.jumbotron .container {
  max-width: 40rem;
}

.album {
  min-height: 50rem; /* Can be removed; just added for demo purposes */
  padding-top: 3rem;
  padding-bottom: 3rem;
  background-color: #f7f7f7;
}

.card {
  float: left;
  width: 33.333%;
  padding: .75rem;
  margin-bottom: 2rem;
  border: 0;
}

.card > img {
  margin-bottom: .75rem;
}

.card-text {
  font-size: 85%;
}

footer {
  padding-top: 3rem;
  padding-bottom: 3rem;
}

footer p {
  margin-bottom: .25rem;
}

/*
* Custom styles
*/

.social-links li a {
  font-size: 1.5em;
}

【问题讨论】:

  • 如果你有“card”、“card-text”、“album”等类的css,请包括它们。
  • @marcusshep,已添加。

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


【解决方案1】:

为您的 index.html.erb 尝试此代码:

<div class="album text-muted">
   <div class="container">
      <div class="row">
         <% @products.each do |product| %>
         <div class="card">
            <%= image_tag("picture_holder")%>
            <strong class="card-text"><%= product.summary %></strong>
            <a href="<%= product_path(product) %>"><%= image_tag("picture_holder")%><a>
            <p class="card-text"><%= product.price %></p>
            <p class="card-text"><%= product.description %></p>
         </div>
         <% end %>
      </div>
      <div class="row">
        <div class="col-md-12">
          <%= paginate @products %>
        </div>
      </div>
   </div>
</div>

它应该可以工作。

【讨论】:

    【解决方案2】:

    我希望您尝试在同一迭代中输出图像和描述。

    <% @products.each do |product| %>
        <div class="card">
            <%= product.image %>
            <p class="card-text">
                <%= product.description %>
            </p>
        </div>
    <% end %>
    

    请注意 here 我可以看到他们正在显示他们的图像

    <img data-src="holder.js/100px280/thumb" 
        alt="100%x280" style="height: 280px; width: 100%; 
        display: block;" src="some/path/" data-holder-rendered="true">
    

    注意这里应用的内联样式。另外,我不确定product.image 是图像路径还是实际二进制图像。如果是前者,则需要将其放入 img 标签的 src= 属性中,类似于他们示例中使用的引导程序。

    <img scr='<%= product.image %>' />
    

    【讨论】:

    • 不幸的是,这没有任何作用。我注意到您没有 ,但如果我尝试删除它,所有数据都会消失。
    • 是的,我从示例中排除了您的大部分代码。试试这个,我要编辑我的答案。请注意,我正在查看 here 并使用 chromes“检查元素”来查看 html 和类的原始结构。
    • marcus,添加img标签只是将div拉伸到整个屏幕,但它仍然显示为一种博客类型的格式,一个在另一个之下,而不是像相册模板中那样的网格系统.
    • 你应用了我上面提到的内联样式吗?他们应该限制每张图片的大小。
    • Marcus 我确实应用了内联样式,这有助于调整图像大小,但它仍然没有显示为网格,而是显示为一个在另一个下方的列表。
    猜你喜欢
    • 2013-08-20
    • 2020-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多