2014-09-13 49 views
0

我使用一些自定義的字體在我的Rails 4.1的應用程序。它們都坐落在這裏:Rails的資產管道:爲什麼我的自定義字體在發展,但不是生產工作?

app/assets/fonts/MyCustomFont.ttf 

我引用他們在我的樣式表(SASS):

@font-face 
    font-family: "MyCustomFont" 
    src: url("MyCustomFont.ttf") format('truetype') 

h1 
    color: #fff 
    font-family: "MyCustomFont" 

在發展模式,它正確地呈現我的字體。當我部署到生產環境,我看到在日誌中提到的字體:

** Execute assets:precompile 
I, [2014-09-12T23:46:40.077333 #12473] INFO -- : Writing /var/www/apps/10121/releases/c655762f076df708896b622c12429c8bf76f21ec/public/assets/MyCustomFont-18108066511bb5b3ddfd0454b738e8d5.ttf 

然而,當我加載頁面在瀏覽器中它不會呈現自定義字體。 Web檢查控制檯顯示錯誤:

Failed to load resource: the server responded with a status of 404 (Not Found) http://example.com/assets/MyCustomFont.ttf

當我在使用日誌中列出的文件名的字體輸入URL:

http://example.com/assets/MyCustomFont-18108066511bb5b3ddfd0454b738e8d5.ttf

然後,瀏覽器觸發文件下載。因此,它似乎只是在錯誤的位置引用文件的問題。

我怎樣才能解決這個問題,什麼是引用的字體,使其在生產工作時的資產編制的正確方法是什麼?

+0

沒有你嘗試運行'耙資產:precompile' – 2014-09-13 04:12:19

+0

正如我所說的,當我部署到生產環境(並給日誌輸出的樣品)的資產被編譯。您不想在開發模式下運行該命令。 – Andrew 2014-09-13 18:43:35

回答

3

預編譯

經典問題 - 它基本上是下降到了Rails的precompilation過程(怎麼指紋文件等),以及您不調用dynamic文件路徑(這是因爲這precompilation過程的結果產生)

你調用以下:

#app/assets/stylesheets/application.css.sass 
@font-face 
    font-family: "MyCustomFont" 
    src: url("MyCustomFont.ttf") format('truetype') 

這將在開發工作,因爲資產文件夾中將提供。

:然而,當這個被推到生產(從而有指紋追加,並移動到文件夾 public/assets),你的靜態參考行不通

相反,你會使用asset_url幫手最好

#app/assets/stylesheets/application.css.sass 
@font-face 
    font-family: "MyCustomFont" 
    src: asset_url("MyCustomFont.ttf") format('truetype') 

這將引用動態文件 - 而不是靜態的 - 這意味着預編譯過程中應努力!

+1

我瞭解到,您也可以撥打'font_url'並得到同樣的結果。 – Andrew 2014-09-13 18:46:48

相關問題