Tom Pinchen
Tom Pinchen

Reputation: 2497

How to put images into an array Ruby on Rails

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>

Upvotes: 2

Views: 2325

Answers (2)

nathanvda
nathanvda

Reputation: 50057

The simple solution is to add a helper. So 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 then you can write the following in your view:

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

Now having the 6 image_x fields for place looks a bit smelly, so I would prefer a nested model instead as Rich Peck proposes, although I understand having the 6 fields is easier to start with.

Upvotes: 1

Richard Peck
Richard Peck

Reputation: 76774

Images

It will all depend on how you've set up the images in the db, and how you'd like to show them in the view

If you have the images in the User model, you could call them like this:

app/controllers/users_controller.rb
Class UsersController < ApplicationController
   def show
       @user = User.find params[:id]
       @images = @user.images
   end
end

This is obviously very generalised - I would recommend you post some context to your question, so we know what you're asking specifically

--

Paperclip

If you're using Paperclip or Carrierwave, you'll basically have the images attached to various objects in your database. For example, you'll have the following:

#app/models/image.rb
Class Image < ActiveRecord::Base
   has_attached_file :attachment
end

This allows you to call:

@images = Image.all
@images.each do |image|
   image.attachment.url #-> image URL
end

This is the standard way to handle images / attachments in Rails, as it ties directly into ActiveRecord. Returning an array of image paths would therefore be a case of calling Model.all or Model.where

Upvotes: 0

Related Questions