Last active
October 25, 2019 13:01
-
-
Save tomeara/6515860 to your computer and use it in GitHub Desktop.
Inline SVG for Rails
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Put this method in your helper file to render inline SVG | |
def inline_svg(path) | |
file = File.open("app/assets/images/#{path}", "rb") | |
raw file.read | |
end |
Combining all this, I added this to my application helper:
def inline_svg(path)
raw Rails.application.assets.find_asset(path + '.svg').to_s
end
Rails.application.assets
became nil when config.assets.compile = false
on production. Does it matter?
How to upload an sag to AWS S3 using Carrierwave? in my rails add, any help will be appreciated thanks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Instead of hard coding the path to
app/assets/images
I suggest to let the asset pipeline manage the file location. UseRails.application.assets.find_asset(path).to_s
instead of reading the file directly.