2015-03-03 34 views
2

= @photo.image.url(以HAML)將編譯根據視圖圖像的URL呼叫軌道路線到CSS(以HAML)

我想使用由標籤產生的圖像鏈接的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到背景網址

+0

你可以,如果你使用的是軌道4'背景圖片試試這個:圖像URL( 「newimg.jpg」)',它應該找到'newimg'在你的'assets/images /'文件夾 – Sontya 2015-03-03 08:40:07

+0

@Sontya,我的問題可能令人困惑:newimg.jpg是一個不相關的圖像,我用它來測試我使用的例子 – KTHero 2015-03-03 08:46:01

+0

無論你的圖像文件的名字是什麼, – Sontya 2015-03-03 08:49:07

回答

0

插值值@photo.image.url並將其添加到字符串

在HAML中呈現標記時

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

CSS

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