Ruby on rails 轨道循环不生成网格
我有一个循环,循环遍历一个故事列表,但它为每个故事生成一行,而不是3列。我错过了什么Ruby on rails 轨道循环不生成网格,ruby-on-rails,Ruby On Rails,我有一个循环,循环遍历一个故事列表,但它为每个故事生成一行,而不是3列。我错过了什么 <div class="row"> <div class="col-md-4"> <% @stories.each do |story| %> <h2><%= story.name.titleize %></h2> <%= image_tag story.pictures.fir
<div class="row">
<div class="col-md-4">
<% @stories.each do |story| %>
<h2><%= story.name.titleize %></h2>
<%= image_tag story.pictures.first.image(:medium).to_s, class: "img-responsive" %>
<%= link_to "View Story", story_path(story) %>
<% end %>
</div>
</div>
谢谢。我想你把每个循环放错地方了。尝试以下方法:
<div class="row">
<% @stories.each do |story| %>
<div class="col-md-4">
<%= content_tag :h2, story.name.titleize %>
<%= image_tag story.pictures.first.image(:medium).to_s, class: "img-responsive" %>
<%= link_to "View Story", story_path(story) %>
</div>
<% end %>
</div>
查看每个片段
@stories = [story1, story2, story3, story4, story5, story6, story7, story8]
@stories.each_slice(3).to_a
#=> [[story1, story2, story3], [story4, story5, story6], [story7, story8]]
我想你可以试试这个:
<% @stories.each_slice(3).to_a.each do |row_stories| %>
<div class="row">
<% row_stories.each do |story| %>
<div class="col-md-4">
<%= content_tag :h2, story.name.titleize %>
<%= image_tag story.pictures.first.image(:medium).to_s, class: "img-responsive" %>
<%= link_to "View Story", story_path(story) %>
</div>
<% end %>
</div>
<% end %>
假设您正在使用引导。您将所有信息嵌套在一个列中,而不是创建三个列。我曾尝试将“col-md-4”放在循环下面,但直到看到您之前放置的位置,它才起作用。这就成功了。非常感谢DaniG2K。这对我来说是新的。不知道它是怎么工作的,但我会玩代码看看它能做什么。谢谢你的回答。