Search code examples
ruby-on-railsruby-on-rails-5rails-activestorage

How can I get content type from an ActiveStorage attachment?


I'm creating a view that features a video stored via ActiveStorage. Currently I'm displaying the video like this:

%video{ controls: true, preload:"metadata" }
    %source{ src: rails_blob_path(@video.source), type: "TODO: Content Type" }

I'd like to find a way to get the content type from the attachment. I've found that I can get to it by using @video.source.attachment.blob.content_typebut that seems so clunky. Is there another simpler way to go about it that resembles video.source.content_type? Unfortunately using the video_tag helper is not a viable solution for me.


Solution

  • Yes, there is a shorter solution: @video.source_blob.content_type.

    I recommend you to look at the source code of ActiveStorage, there you can see all the available methods and possibilities that are not always well documented.