我需要循环存储在我的数据库中的图像。对于上传,我使用回形针。在这里,您可以看到我的变量从控制器传递到视图。
true
这是一个硬编码的视图,看起来很完美,但我需要创建一个打破一切的循环。这是我的观点:
@slides
=> [#<Content id: 1, title: "Slider1", content: "", modal_name: "slider", created_at: "2016-09-07 13:01:38", updated_at: "2016-09-07 13:01:38", content_type_id: "2", image_file_name: "family.jpg", image_content_type: "image/jpeg", image_file_size: 862276, image_updated_at: "2016-09-07 13:01:37">, #<Content id: 2, title: "Slider2", content: "", modal_name: "slider", created_at: "2016-09-07 13:02:17", updated_at: "2016-09-07 13:02:17", content_type_id: "2", image_file_name: "go.png", image_content_type: "image/png", image_file_size: 449856, image_updated_at: "2016-09-07 13:02:17">, #<Content id: 18, title: "family", content: "", modal_name: "family", created_at: "2016-10-04 12:53:15", updated_at: "2016-10-04 12:53:15", content_type_id: "2", image_file_name: "family.jpg", image_content_type: "image/jpeg", image_file_size: 862276, image_updated_at: "2016-10-04 12:53:15">, #<Content id: 19, title: "go", content: "", modal_name: "go", created_at: "2016-10-04 12:53:32", updated_at: "2016-10-04 12:53:32", content_type_id: "2", image_file_name: "go.png", image_content_type: "image/png", image_file_size: 449856, image_updated_at: "2016-10-04 12:53:32">, #<Content id: 20, title: "family2", content: "", modal_name: "family2", created_at: "2016-10-04 12:53:53", updated_at: "2016-10-04 12:53:53", content_type_id: "2", image_file_name: "family.jpg", image_content_type: "image/jpeg", image_file_size: 862276, image_updated_at: "2016-10-04 12:53:52">]
答案 0 :(得分:0)
我认为这应该有效..因为你必须循环 Any
..
INDICATORS
以下是<!-- Indicators -->
<ol class="carousel-indicators">
<% @slides.each_with_index do |slide, i| %>
<li data-target="#mycarousel" data-slide-to=#{i) class="#{'active' if i == 0}"></li>
<% end %>
</ol>
<!-- Wrapper for slides -->
<div class="carousel-inner">
<% @slides.each_with_index do |slide, i| %>
<div class="item #{'active' if i == 0}">
<%= image_tag('user/' + slide.image_file_name , class: 'fill img-responsive') %>
<div class="carousel-caption">
<h2>Caption 1</h2>
</div>
</div>
<% end %>
</div>
如何运作以及each_with_index
&gt;之间的差异的一个很好的示例STACKOVERFLOW