如何获得随机 background_image

How to get a random background_image

我有一个获取随机图像的辅助方法:

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

在我看来:

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

但是在我的 html 中我得到了一个错误的 url(没有斜线):

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

你试过用'替换里面的"吗?

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

变成

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