2016-10-04 56 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