将rails路由调用到CSS(在haml中)

Call a rails route into CSS (in haml)

本文关键字:haml CSS rails 路由 调用      更新时间:2023-09-26

= @photo.image.url(在haml中)将根据视图编译图像的url

我想使用下面CSS中的标签生成的图像链接,但这不起作用。

#BG {
  background: url( HERE!!! ) no-repeat center center fixed;
  -webkit-background-size: cover;
    -moz-background-size: cover;
    -o-background-size: cover;
    background-size: cover;
    height: 400px;
}

我直接在页面上尝试了javascript,用新的图像替换硬连接的图像来测试它。。。但那没用。我已经玩过下面的代码,并尝试了很多不同的版本。

:javascript
  document.getElementById('BG').style.background = "url('newimg.jpg') no-repeat center center fixed"

以下是将要使用的Haml代码:

 #BG
  This div should have the image as a background
  %p= @photo.title
.container
  .jumbotron

我需要一个解决方案来将图像url转换为背景url

插入值@photo.image.url并将其添加到字符串

在HAML中呈现标签时

#BG{style: "background: url(#{@photo.image.url}) no-repeat"}

CSS

#BG {background: url("#{@photo.image.url}")}