Ben
Ben

Reputation: 712

How to get a random background_image

I have an helper method to get a random image :

  def home_random_image
    image_path = "app/assets/images/home/"
    image_files = Dir.glob("#{image_path}*")
    image_files.sample
  end

In my view :

style="background-image: url("<%= home_random_image %>");"

But in my my html I get a bad url (without slashs) :

style="background-image: url("app asset images home img.jpg")

Upvotes: 0

Views: 36

Answers (1)

Balastrong
Balastrong

Reputation: 4464

Have you tried by replacing the inner " with '?

style="background-image: url("<%= home_random_image %>");"

becomes

style="background-image: url('<%= home_random_image %>');"

Upvotes: 1

Related Questions