How to display SVG as images with Active Storage

Add this code to your config/application.rb:

# Hack for allowing SVG files. While this hack is here, we should **not**
# allow arbitrary SVG uploads. https://github.com/rails/rails/issues/34665

ActiveStorage::Engine.config
.active_storage
.content_types_to_serve_as_binary
.delete('image/svg+xml')

You can delete comments of course :). Hope it helps.


Svg are considered binary content types, so that is why Active storage svg links are represented as download attachment links. You can see all content_types and limitations at this link https://github.com/rails/rails/blob/master/activestorage/lib/active_storage/engine.rb.

More about this limitation and explanation can be found here: http://github.com/jdelStrother/rails/commit/06ab7b27ea1c1ab357085439abacdb464f6742bf. I'll post an issue for this. Maybe they will fix it in the future