How to put images into an array Ruby on Rails -


i have ruby on rails app allows user save 6 images can viewed in carousel.

the images saved strings image, image_2, image_3, image_4, image_5, image_6.

i want able write 'for' loop display of images in carousel.

what best method of combining of these image strings array can looped through , outputted carousel?

further details

i calling images below works 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 able turn have below simple loop go through each of 6 image objects , return ones there. more this:

<div style="position:relative">   <div id="home-carousel" class="carousel">     <div class="carousel-inner">       <% @place.images.each |image| %>         <div class="item">           <%= image_tag image %>         </div>       <% end %>     </div>   </div> </div> 

the simple solution add helper. in helpers/places_helper.rb write

module placeshelper    def get_carrousel_images(place)     [       @place.image_1,       @place.image_2,       @place.image_3,       @place.image_4,       @place.image_5,       @place.image_6     ].select {|img| img.present? }   end 

and can write following in view:

<div style="position:relative">   <div id="home-carousel" class="carousel">     <div class="carousel-inner">       <% get_carrousel_images(@place).each |image| %>         <div class="item">           <%= image_tag image %>         </div>       <% end %>     </div>   </div> </div> 

now having 6 image_x fields place looks bit smelly, prefer nested model instead rich peck proposes, although understand having 6 fields easier start with.


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -