-1

Ruby on railsアプリケーションのPostgreSQLデータベースに保存された画像を使って、ブートストラップカルーセルに画像を読み込もうとしています。私はフロントエンドでERBを使用しています。何も以下のコードが現れると...データベースからブートストラップカルーセルにイメージを読み込むために、私の三項演算子がERBのクラスで動作しないのはなぜですか?

<div id="myCarousel" class="carousel slide" data-ride="carousel"> 
<!-- Indicators --> 
<ol class="carousel-indicators"> 
    <% @post.first(3).each do |image, index| %> 
    <li data-target="#myCarousel" data-slide-to="<%= index %>" class="<%= index == 0 ? 'active' : '' %>"></li> 
    <% end %> 
</ol> 

<!-- Wrapper for slides --> 

<div class="carousel-inner" role="listbox"> 
<% @post.first(3).each do |image, index| %> 
    <div class="item <%= index == 0 ? 'active' : '' %>"> 
    <%= link_to image_tag(image.image.url, class:"images") %> 
    <div class=""> 
     <h3><%= index %></h3> 
    </div> 
    </div> 
<% end %> 
</div> 


<!-- Left and right controls --> 
<a class="left carousel-control" href="#myCarousel" role="button" data-slide="prev"> 
<span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span> 
<span class="sr-only">Previous</span> 
</a> 
<a class="right carousel-control" href="#myCarousel" role="button" data-slide="next"> 
    <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span> 
    <span class="sr-only">Next</span> 
</a> 
</div> 
+0

あなたが役に立った場合、回答を受け入れることができますか? – Gabbar

+0

ここではインデックス変数はゼロです。ポストオブジェクトを繰り返し処理するために、each_with_indexをそれぞれの代わりに使用してください。 –

答えて

1

理由は、あなたの三項演算子であること....私はそれが私のクラスで私の三項演算子とは何かを持っていると考えているが、私は、正確な問題が何であるかわかりませんそれはあなたがそうここeach_with_indexの代わりに、各

で行う必要がある作業を行うためにindexを取得されていませんして行うには正しい方法です:

<div id="myCarousel" class="carousel slide" data-ride="carousel"> 
<!-- Indicators --> 
<ol class="carousel-indicators"> 
    <% @post.first(3).each_with_index do |image, index| %> <!--use each_with_index --> 
    <li data-target="#myCarousel" data-slide-to="<%= index %>" class="<%= index == 0 ? 'active' : '' %>"></li> 
    <% end %> 
</ol> 

<!-- Wrapper for slides --> 

<div class="carousel-inner" role="listbox"> 
<% @post.first(3).each_with_index do |image, index| %> <!--use each_with_index --> 
    <div class="item <%= index == 0 ? 'active' : '' %>"> 
    <%#= link_to image_tag(image.image.url, class:"images") %> <!--use image_tag --> 
    <%=image_tag image.image.url ,class: "images"%> 
    <div class=""> 
     <h3><%= index %></h3> 
    </div> 
    </div> 
<% end %> 
</div> 


<!-- Left and right controls --> 
<a class="left carousel-control" href="#myCarousel" role="button" data-slide="prev"> 
<span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span> 
<span class="sr-only">Previous</span> 
</a> 
<a class="right carousel-control" href="#myCarousel" role="button" data-slide="next"> 
    <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span> 
    <span class="sr-only">Next</span> 
</a> 
</div> 

それが必要以上画像がある場合、私はちょうど、問題を修正しました今仕事。さらなるガイダンスについて私に知らせてください。

+0

まさに問題でした。ありがとうございました! –

関連する問題