I have a Ruby on Rails app that allows a user to save up to 6 images that can then be viewed in a carousel.
The images are saved as strings as image, image_2, image_3, image_4, image_5, image_6.
I want to be able to write a 'for' loop to display all of the images in my carousel.
What is the best method of combining all of these image strings into an array so that they can then be looped through and outputted by the carousel?
Further Details
I am currently calling the images like below which works but isn't particularly DRY.
<div style="position:relative">
<div id="home-carousel" class="carousel">
<div class="carousel-inner">
<div class="item active">
<%= image_tag @place.image %>
</div>
<% if @place.image_2.present? %>
<div class="item">
<%= image_tag @place.image_2 %>
</div>
<% end %>
<% if @place.image_3.present? %>
<div class="item">
<%= image_tag @place.image_3 %>
</div>
<% end %>
<% if @place.image_4.present? %>
<div class="item">
<%= image_tag @place.image_4 %>
</div>
<% end %>
<% if @place.image_5.present? %>
<div class="item">
<%= image_tag @place.image_5 %>
</div>
<% end %>
<% if @place.image_6.present? %>
<div class="item">
<%= image_tag @place.image_6 %>
</div>
<% end %>
</div>
</div>
</div>
I would like to be able to turn what I have below into a simple for loop that will go through each of the 6 image objects and return the ones that are there. Something more like this:
<div style="position:relative">
<div id="home-carousel" class="carousel">
<div class="carousel-inner">
<% @place.images.each do |image| %>
<div class="item">
<%= image_tag image %>
</div>
<% end %>
</div>
</div>
</div>