Rails every method with html change (bootstrap carousel)
So I am trying to find a solution for Bootstrap 3 carousel in Rails 4.
I have it:
<li> <a id="carousel-selector-0" class="selected">
<%= image_tag @gallery.images[0].photo.url(:thumb), class: 'img-responsive' %>
</a></li>
<li> <a id="carousel-selector-1">
<%= image_tag @gallery.images[1].photo.url(:thumb), class: 'img-responsive' %>
</a></li>
...etc...
I'm looking for a method of each type that accounts for the variable number of images in the gallery.
Something where the first photo looks like this:
<li> <a id="carousel-selector-0" class="selected">
<%= image_tag @gallery.images[0].photo.url(:thumb), class: 'img-responsive' %>
</a></li>
And after it they look:
<li> <a id="carousel-selector-1">
<%= image_tag @gallery.images[1].photo.url(:thumb), class: 'img-responsive' %>
</a></li>
.......
With each quantity increasing by one for each photograph, of course.
Thanks for the help!
Decision
Thank you for your creativity!
Here's what worked:
<% @gallery.images.each_with_index do |image, index| %>
<li> <a id="carousel-selector-<%=index%>" class="<%= 'selected' if index == 0%>">
<%= image_tag image.photo.url(:thumb), class: 'img-responsive' %>
</a></li>
<% end%>
And if anyone is looking for a solution for the .carousel-inner div (almost the same):
<div class="carousel-inner">
<% @gallery.images.each_with_index do |image, index| %>
<div class="item <%= 'active' if index == 0%>" data-slide-number="<%= index %>">
<%= image_tag image.photo.url(:large), class: 'img-responsive' %>
</div>
<% end%>
</div>
+3
source to share
1 answer
Something like this should work:
<% @gallery.images.each_with_index do |image, index| %>
<li> <a id="carousel-selector-<%=index%>" class="<%='selected' if index == 0%>">
<%= image_tag image.photo.url(:thumb), class: 'img-responsive' %>
</a></li>
<% end%>
Revised: because I didn't notice the selected tag difference li
. Now I am usingeach_with_index
+3
source to share