2014-04-06 64 views
2

我試圖按照本指南:http://css-tricks.com/svg-sprites-use-better-icon-fonts包括ERB爲Rails

克里斯指出剛剛開幕body標籤後注入SVG。在PHP中,這很簡單,但我使用Rails,所以有一個.html.erb文件。

如何使用此文件包含該文件?我嘗試過資產助手,評估和渲染。似乎沒有任何工作?

+4

試試這個https://gist.github.com/tomeara/6515860 –

+0

你應該把這個作爲答案 – Alexis

回答

6

這屬於Ruby Racer。

inline_svg_helper.rb

def inline_svg(path) 
    File.open("app/assets/images/#{path}", "rb") do |file| 
    raw file.read 
    end 
end 
0

<%= image_tag 'file.svg' %>似乎並沒有在Chrome瀏覽器中工作,所以我試圖render以包括以下方式SVG文件,和它的工作對我來說。

<%= render inline: (render file: Rails.root.join('app', 'assets', 'images', 'file.svg')) %> 

我還有一個問題,這是一個好的(或安全的)方式?

+1

我發現我不需要'render inline:',只是'render file:'工作正常。 – Yogh