我在尝试使用引导轮播显示附加到招聘帖的多个图像时遇到了一些问题。以下是没有轮播模板的原始代码:
<% if @job.images.attached? %>
<% (0...@job.images.count).each do |image| %>
<%= image_tag(@job.images[image]) %>
<% end %>
<% else %>
<%= image_tag "missing.jpg" %>
<% end %>
下面是我从 w3 学校使用的引导轮播示例,当我在我的应用程序上测试它时,它工作正常。
<div id="demo" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<ul class="carousel-indicators">
<li data-target="#demo" data-slide-to="0" class="active"></li>
<li data-target="#demo" data-slide-to="1"></li>
<li data-target="#demo" data-slide-to="2"></li>
</ul>
<!-- The slideshow -->
<div class="carousel-inner">
<div class="item active">
<img src="la.jpg" alt="Los Angeles">
</div>
<div class="item">
<img src="chicago.jpg" alt="Chicago">
</div>
<div class="item">
<img src="ny.jpg" alt="New York">
</div>
</div>
<!-- Left and right controls -->
<a class="carousel-control-prev" href="#demo" data-slide="prev">
<span class="carousel-control-prev-icon"></span>
</a>
<a class="carousel-control-next" href="#demo" data-slide="next">
<span class="carousel-control-next-icon"></span>
</a>
</div>
到目前为止,我已经尝试过:
<div id="demo" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<ul class="carousel-indicators">
<% (0...@job.images.count).each do |image| %>
<li data-target="#demo" data-slide-to=#{image} class="active"></li>
<% end %>
</ul>
<!-- The slideshow -->
<div class="carousel-inner">
<% (0...@job.images.count).each do |image| %>
<div class="item active">
<%= image_tag(@job.images[image]) %>
</div>
<% end %>
</div>
<!-- Left and right controls -->
<a class="carousel-control-prev" href="#demo" data-slide="prev">
<span class="carousel-control-prev-icon"></span>
</a>
<a class="carousel-control-next" href="#demo" data-slide="next">
<span class="carousel-control-next-icon"></span>
</a>
</div>
图像相互堆叠,当我单击下一张幻灯片时,所有图像都消失了。我不确定在数据幻灯片类中到底要放什么,我怀疑这是问题的原因。
更新
当图片滑块自动播放时,此代码有效,但手动单击图片滑块会导致整个轮播消失:
<div id="demo" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<ul class="carousel-indicators">
<% @job.images.each_with_index do |image, index| %>
<li data-target="#demo" data-slide-to=#{index} <%= index == 0 ? 'class="active"' : '' %>></li>
<% end %>
</ul>
<!-- The slideshow -->
<div class="carousel-inner">
<% @job.images.each_with_index do |image, index| %>
<div class="item <%= index == 0 ? 'active' : '' %>">
<%= image_tag image %>
</div>
<% end %>
</div>
<!-- Left and right controls -->
<a class="carousel-control-prev" href="#demo" data-slide="prev">
<span class="carousel-control-prev-icon"></span>
</a>
<a class="carousel-control-next" href="#demo" data-slide="next">
<span class="carousel-control-next-icon"></span>
</a>
</div>
首先,看起来您正在为幻灯片中的每个图像提供active
类。其次,有更简单的方法来迭代图像。
这样的事情应该有效:
<div id="demo" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<ul class="carousel-indicators">
<% @job.images.each_with_index do |image, index| %>
<li data-target="#demo" data-slide-to=#{index} <%= index == 0 ? 'class="active"' : '' %>></li>
<% end %>
</ul>
<!-- The slideshow -->
<div class="carousel-inner">
<% @job.images.each_with_index do |image, index| %>
<div class="item <%= index == 0 ? 'active' : '' %>">
<%= image_tag image %>
</div>
<% end %>
</div>
<!-- Left and right controls -->
<a class="carousel-control-prev" href="#demo" data-slide="prev">
<span class="carousel-control-prev-icon"></span>
</a>
<a class="carousel-control-next" href="#demo" data-slide="next">
<span class="carousel-control-next-icon"></span>
</a>
</div>