2016-10-04 35 views
0

我需要循环存储在我的数据库中的图像。对于上传,我使用回形针。在这里你可以看到我的变量从控制器传递到视图。如何循环导轨图像引导轮播

@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">] 

这是一个硬编码的视图,看起来很完美,但我需要创建一个打破一切的循环。这是我的看法:

<header id="myCarousel" class="carousel slide"> 
    <!-- Indicators --> 
    <ol class="carousel-indicators"> 
    <li data-target="#myCarousel" data-slide-to="0" class="active"></li> 
    <li data-target="#myCarousel" data-slide-to="1"></li> 
    <li data-target="#myCarousel" data-slide-to="2"></li> 
    </ol> 

    <!-- Wrapper for slides --> 
    <div class="carousel-inner"> 
    <div class="item active"> 
     <%= image_tag('user/go.png', class: 'fill img-responsive') %> 
     <div class="carousel-caption"> 
     <h2>Caption 1</h2> 
     </div> 
    </div> 

    <div class="item"> 
     <%= image_tag('user/family.jpg', class: 'fill img-responsive') %> 
     <div class="carousel-caption"> 
     <h2>Caption 2</h2> 
     </div> 
    </div> 
    <div class="item"> 
     <%= image_tag('user/family.jpg', class: 'fill img-responsive') %> 
     <div class="carousel-caption"> 
     <h2>Caption 3</h2> 
     </div> 
    </div> 
    </div> 

    <!-- Controls --> 
    <a class="left carousel-control" href="#myCarousel" data-slide="prev"> 
    <span class="icon-prev"></span> 
    </a> 
    <a class="right carousel-control" href="#myCarousel" data-slide="next"> 
    <span class="icon-next"></span> 
    </a> 
</header> 
+0

你试图用'image_tag'循环'div.item'吗?你可以在你的尝试中添加代码吗? – liborza

+0

是的,它打破了滑块 – user3696668

+0

你是说上面的代码工作正常吗? – bntzio

回答

0

我认为这应该工作。因为我只好也环路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运作以及如何之间的差异each>STACKOVERFLOW

+0

好的,现在我有所有的图像,但滑块并没有做出循环 – user3696668

+0

没有完成循环或显示图像? – liborza