简体   繁体   English

如何使Ruby on Rails将数据显示为Bootstrap网格?

[英]How do I get Ruby on Rails to display data as a Bootstrap grid?

I am applying the Bootstrap 4 album template to a Ruby on Rails website I am developing. 我正在将Bootstrap 4专辑模板应用于我正在开发的Ruby on Rails网站。 The original album template looks like this: 原始相册模板如下所示:

在此处输入图片说明

But mine is not displaying like that. 但是我的并不是那样。 It's displaying as a list, one beneath the other, similar to what a blog page would look like. 它显示为列表,一个在另一个的下面,类似于博客页面的外观。

I believe the file where I need to make the necessary changes is here in app/views/products/index.hmtl.erb: 我相信我需要进行必要更改的文件位于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) %>

I am not missing any of the Bootstrap classes from the original template, so I am wondering if its my code that is causing it not to render appropriately. 我没有从原始模板中丢失任何Bootstrap类,所以我想知道它的代码是否导致其无法正确呈现。

Here is the stylesheets/products.scss file: 这是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;
}

I'd like for you to try to output the image and the description in the same iteration. 我希望您尝试在同一迭代中输出图像和说明。

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

Note that here I can see they are displaying their images like so 请注意, 在这里我可以看到他们像这样显示图像

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

Notice the inline styles that are being applied here. 请注意此处应用的内联样式。 Also, I'm not sure if product.image is a path to an image or an actual binary image. 另外,我不确定product.image是图像的路径还是实际的二进制图像。 if it's the former you'll need to put it into the src= attribute of an img tag similar to the one bootstrap uses in their example. 如果是前者,则需要将其放入img标签的src=属性,类似于其示例中使用的引导程序。

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

try this code for your index.html.erb: 为您的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>

it should work. 它应该工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Ruby on Rails如何显示与特定行关联的数据 - Ruby on rails How do I display data associated with a specific row 在Ruby on Rails 5.2中,如何使我的网站显示“欢迎使用Rails”而不是“ /的索引”? - How do I get my website to display Welcome to Rails instead of Index of / in Ruby on Rails 5.2? 如何在Rails循环中的ruby中有效地实现引导切换数据? - How do I effectively implement bootstrap toggle data in ruby on rails loop? 如何将Twitter Bootstrap与Ruby on Rails 3.2.12结合使用? - How do I use Twitter Bootstrap with Ruby on Rails 3.2.12? 如何将Bootstrap轮播添加到Ruby on Rails应用程序? - How do i add a Bootstrap carousel to Ruby on rails app? 如何从列表中的学生那里获得ID,并在另一页上显示学生的信息? Ruby on Rails - How do I get an id from a student in a list and display it the student's information on another page? Ruby on Rails 如何在Ruby on Rails网站的HTML表的左右列中显示数据? - How do I display data in a Ruby on Rails site in left and right columns of HTML table? 如何在Rails视图中的红宝石中以日历格式显示.ics数据? - How do I display .ics data in calendar format, in a ruby on rails view? 建立多对多连接后-如何显示数据 - ruby on rails - after establishing many to many connection - how do I display data 我如何从其他网站获取元数据并在我的红宝石上显示呢? - How do I grab meta data from other websites and display on mine in ruby on rails?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM