In my web site, I created a carousel. Just now, I realized that I most likely repeated myself with this code. I would like to learn how to DRY up this code.
Here is my view file (.html.erb) format:
<div class="carousel-inner">
<div class="item active">
<%= link_to image_tag(image_path("#{@title}/1.jpg")), home_path %>
</div>
<div class="item">
<%= link_to image_tag(image_path("#{@title}/2.jpg")), home_path %>
</div>
<div class="item">
<%= link_to image_tag(image_path("#{@title}/3.jpg")), home_path %>
</div>
<div class="item">
<%= link_to image_tag(image_path("#{@title}/4.jpg")), home_path %>
</div>
<div class="item">
<%= link_to image_tag(image_path("#{@title}/5.jpg")), home_path %>
</div>
<div class="item">
<%= link_to image_tag(image_path("#{@title}/6.jpg")), home_path %>
</div>
<div class="item">
<%= link_to image_tag(image_path("#{@title}/7.jpg")), home_path %>
</div>
<div class="item">
<%= link_to image_tag(image_path("#{@title}/8.jpg")), home_path %>
</div>
<div class="item">
<%= link_to image_tag(image_path("#{@title}/9.jpg")), home_path %>
</div>
</div>
I'm thinking you could use a for
loop like this:
<% x = 1 %>
<% while x < 10 %>
<%= link_to image_tag(image_path("#{@title}#{x}.jpg")), home_path %>
<% x += 1 %>
<% end %>