Question

In my view I have:

 <div class="item active">
   <%= image_tag "2.jpg",size: "500x300", class: "no-resize" %> 
   <div class="carousel-caption">
     Some tag.
   </div>
 </div>
 <% @photos.each do |photo|%>
 <div class="item">
   <%= image_tag photo.attachment.url,size: "500x300",class: "no-resize" %> 
   <div class="carousel-caption">
     Some tag.
   </div>
 </div>
<% end %>

Is there DRYer way of accomplishing this (as the only change I make is adding the class 'active' for the first photo) i.e. can I put the first item within the loop and detect the first iteration?

Was it helpful?

Solution

Try each_with_index

<% @photos.each_with_index do |photo, i |%>
 <div class="item <%= "active" if i.zero? %>">
   <%= image_tag photo.attachment.url,size: "500x300",class: "no-resize" %> 
   <div class="carousel-caption">
    Some tag.
   </div>
 </div>
<% end %>

OTHER TIPS

['apple', 'orange', 'pear'].each_with_index do |fruit, index|
  p "first is #{fruit}" if index == 0
  p fruit
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top