Vinicius Spader
Vinicius Spader

Reputation: 113

What's the best way to store images for a carousel?

I'm using Ruby on Rails. I'll have two carousels on my landing page, and I want to know what is the best way to store and display these images:

Option One

Put the images in assets/images and show them in the carousel. If this is a good way, can I loop through the images in the folder or I'll have to specify each one of the files?

Option Two

Create a model with an image column and loop through the images that have some specific attribute?

Other Three

Something else

Thanks!

Upvotes: 3

Views: 1444

Answers (1)

senfo
senfo

Reputation: 29026

I'd recommend you not place them directly in the assets/images directory because you will likely add application-specific images to the app at some point that you won't want in your carousel. A folder like app/assets/images/carousel, for example, allows you to more easily determine which images belong in the carousel.

In your controller, add something like the following:

@images = Dir.glob("app/assets/images/carousel/*.{gif,jpg,png}")

Then, in your view, you could add something like the following (assuming ERB)

<% @images.each do |image| %>
   <%= image_tag "carousel/#{image.split('/').last}" %>
<% end %>

Upvotes: 8

Related Questions