Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Inline SVG for Rails

View inline_svg_helper.rb
1 2 3 4 5 6
# 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

This is extremely helpful sir, thank you very much.

p886 commented

Thanks for the snippet, it is good practice to close a file after you use it. I would therefore use the block form of File#open so that the file automatically get's closed after we read from it:

def inline_svg(path)
    File.open("app/assets/images/#{path}", "rb") do |file|
    raw file.read
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.