Showing Specific image from Dir.glob index page

43 Views Asked by At

I have a products controller where i show all my images available in a certain folder:

def index
  @images = Dir.glob("app/assets/images/database/*.jpg")
end

Now i'm trying to link each image to an individual dynamic show page:

def show
  @images = Dir.glob("app/assets/images/database/" + params[:id] + ".jpg")
end

Sadly i get an error "no implicit conversion of nil into String" for quite a while now.

Ideally i want to automatic render all the images from the database folder on the index page and link them individually to the show action where i use exifr to get specific information about each image.

1

There are 1 best solutions below

1
Alexander Shlenchack On
def show
  @images = Dir.glob("app/assets/images/database/" + params[:id] ||
                     "no-image" + ".jpg")
end

It's not a good practice. You should use any image uploader and set teh default image. E.g. for CarrierWave:

class MyUploader < CarrierWave::Uploader::Base
  def default_url(*args)
    "/images/fallback/" + [version_name, "default.png"].compact.join('_')
  end
end