How do I display SVG image in Rails?

后端 未结 7 2142
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-04 16:45

I have a svg image on /app/asssets/images/symbols.svg with this as contents.



        
7条回答
  •  灰色年华
    2021-01-04 17:28

    Displaying SVGs (scalable vector graphics) in a Rails app

    Specifically include the SVG filetype in asset compilation

    production.rb 
    
      config.assets.precompile += %w( '.svg' )  
    
      # Must include to get inline SVGs to work in deploy
      config.assets.css_compressor = :sass
    

    Create a helper to display the SVG

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

    Invoke the SVG processing helper in your view

    <%= show_svg('symbols.svg') %>
    

    From your question I'm not 100% clear about your implementation, but these steps should get you to the point where your SVG image is visible. Once you have the SVG visible, you can apply CSS.

提交回复
热议问题