我有一个循环遍历一个故事列表,但它每个故事产生一行而不是3列。我错过了什么?
<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>
感谢。
答案 0 :(得分:1)
我认为您将每个循环放在错误的位置。尝试类似:
<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>
答案 1 :(得分:1)
看看each_slice
@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 %>