Reputation: 119
I've been trying to add a carousel that can detect images being uploaded and display them accordingly in the carousel. Like if i upload 1 it will only show that, if 2 then so on. I can't seem to really know what to do. i have been trying code after code but it never works. i guess i should loop them somehow but i just cant get it through my head as to what to read to make it happen.
i used this code before wanting to use the carousel but it doenst work if i try to put it inside the carousel
<% ([email protected]).each do |photo| %>
<%= image_tag(@ad.photos[photo]) %>
<% end %>
and this is a simple carousel
<div class="container my-4">
<hr class="my-4">
<!--Carousel Wrapper-->
<div id="carousel-thumb" class="carousel slide carousel-fade carousel-thumbnails" data-ride="carousel">
<!--Slides-->
<div class="carousel-inner" role="listbox">
<div class="carousel-item active">
<img class="d-block w-100" src=".." alt="First slide">
</div>
<div class="carousel-item">
<img class="d-block w-100" src=".." alt="Second slide">
</div>
<div class="carousel-item">
<img class="d-block w-100" src=".." alt="Third slide">
</div>
</div>
<!--/.Slides-->
<!--Controls-->
<a class="carousel-control-prev" href="#carousel-thumb" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#carousel-thumb" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
<!--/.Controls-->
<ol class="carousel-indicators">
<li data-target="#carousel-thumb" data-slide-to="0" class="active"> <img class="d-block w-100"></li>
<li data-target="#carousel-thumb" data-slide-to="1"><img class="d-block w-100" ></li>
<li data-target="#carousel-thumb" data-slide-to="2"><img class="d-block w-100" ></li>
</ol>
</div>
<!--/.Carousel Wrapper-->
i tried reusing the code into the wrapper but it doesnt work
Upvotes: 2
Views: 575
Reputation: 46
The following code should work. I considered the photos as being saved in Cloudinary, if not, just change the code in the "img" tag to display the photos in the correct way.
<% if @ad.photos.size == 1 %>
<img class="d-block w-100" src="<%= cl_image_path @ad.photos.first.key %>" alt="photo" >
<% elsif @ad.photos.size > 1 %>
<div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel">
<ol class="carousel-indicators">
<% @ad.photos.size.times do |i| %>
<li data-target="#carouselExampleIndicators" data-slide-to="<%= i %>" class="<%= "active" if i.zero? %>"></li>
<% end %>
</ol>
<div class="carousel-inner">
<% @ad.photos.each_with_index do |photo, index| %>
<div class="carousel-item <%= "active" if index.zero? %>">
<img class="d-block w-100" src="<%= cl_image_path photo.key %>" alt="<%= index.ordinalize %> image">
</div>
<% end %>
</div>
<a class="carousel-control-prev" href="#carouselExampleIndicators" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#carouselExampleIndicators" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
<% end %>
Upvotes: 2