[英]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.