2013-05-26 35 views
0

這裏的CSS塊,我加載CSS字體:導軌 - 路徑字體不生產工作,但不會對本地主機

@font-face { 
    font-family: 'HelveticaNeueRegular'; 
    src: url('../fonts/helveticaneue/helveticaneue-roman.eot'); 
    src: url('../fonts/helveticaneue/helveticaneue-roman.eot?#iefix') format('embedded-opentype'), 
     url('../fonts/helveticaneue/helveticaneue-roman.woff') format('woff'), 
     url('../fonts/helveticaneue/helveticaneue-roman.ttf') format('truetype'), 
     url('../fonts/helveticaneue/helveticaneue-roman.svg#helvetica_neueregular') format('svg'); 
    font-weight: normal; 
    font-style: normal; 
} 

的字體位於/app/assets/fonts目錄。

在本地主機上,當我加載頁面時,字體未加載,但在Heroku上都加載了正確的字體。

出了什麼問題?

+1

您可能需要使用'asset_path' helper。 http://guides.rubyonrails.org/asset_pipeline.html#css-and-erb – doesterr

回答

0

這與順便做資產管道工程:

  • 在開發環境中,所有的資產都是動態服務器D從/app/assets/...
  • 在生產環境中,資產被編譯,並且移動到/public/assets/,從那裏Web服務器可以獲取現在的靜態文件。

我懷疑fonts.css(意外?)中的路徑指向生產環境中的文件。

爲了解決你的情況,你基本上有兩種選擇:

  1. 移動字體文件到您的/public目錄(如/public/fonts),並從那裏引用文件:

    @font-face { 
        font-family: 'HelveticaNeueRegular'; 
        src: url('/fonts/helveticaneue/helveticaneue-roman.eot'); 
        src: url('/fonts/helveticaneue/helveticaneue-roman.eot?#iefix') format('embedded-opentype'), 
         url('/fonts/helveticaneue/helveticaneue-roman.woff') format('woff'), 
         url('/fonts/helveticaneue/helveticaneue-roman.ttf') format('truetype'), 
         url('/fonts/helveticaneue/helveticaneue-roman.svg#helvetica_neueregular') format('svg'); 
        font-weight: normal; 
        font-style: normal; 
    } 
    
  2. 使用Rails的資產助手。一個很好的介紹可以發現here

4

你看到這種情況的原因是,在生產環境中的所有資產被編譯並放入一個文件夾/assets/asset-name。據說,css和type面都在同一個文件夾中,並且可以通過相對路徑訪問。在開發環境中,資源不會被編譯,並且可以在/assets/asset-type/asset-name處訪問,這意味着CSS和類型面不會位於同一個文件夾中。

爲了克服這個障礙,Rails有一個叫asset-url的驚人幫手。

因此,對於你的例子:

@font-face { 
font-family: 'HelveticaNeueRegular'; 
src: asset-url('helveticaneue-roman.eot'); 
src: asset-url('helveticaneue-roman.eot?#iefix') format('embedded-opentype'), 
    asset-url('helveticaneue/helveticaneue-roman.woff') format('woff'), 
    asset-url('helveticaneue/helveticaneue-roman.ttf') format('truetype'), 
    asset-url('helveticaneue/helveticaneue-roman.svg#helvetica_neueregular')  format('svg'); 
font-weight: normal; 
font-style: normal; 
} 

此外,因爲你加下資產Fonts文件夾,你將需要添加到您的資源路徑:在config/application.rb

+0

我不明白,但我試圖使用'asset-url' helper,當我加載頁面,我沒有得到任何錯誤,但在頁面上沒有使用這些字體...該死! – user984621

+0

你是說你沒有收到任何錯誤,並且沒有使用字體? –

+0

沒錯。在Rails日誌文件中,JS控制檯中都沒有錯誤。 – user984621

0

config.assets.paths << "#{Rails.root}/app/assets/fonts"我終於想通了這個問題 - 進入config/application.rb加入以下內容:

config.assets.paths << Rails.root.join("app", "assets", "fonts") 
config.assets.precompile += %w(.svg .eot .woff .ttf) 
+0

啊!是的,那是因爲你在資產下添加了一個字體文件夾。所以它需要被添加到資產路徑。我也會將其添加到我的答案中。 –

+0

除了最初忘記設置資產路徑之外,我是否回答您的問題? –

相關問題