I recently wanted to upload videos to my blog, but to my surprise, ActionText with Trix would not enable video playback. The purpose of this tutorial is to show you how to enable video playback in your content. To better understand ActionText, you can check out the documentation here.
Let’s get started
In ActionText, you will have to understand that every attachment is a blob, located at app/views/active_storage/blobs/_blob.html.erb. In the below partial, we perform a check to confirm if an attachment is a static image or a video and in the case it’s the latter, we will add a video tag with certain parameters.
I chose to disable the file size part of the caption, I don’t see it befitting my use case in the blog, it’s a small minor UX improvement.
I used VideoJS for the ability to play video, a cool JavaScript library. You will need to include this in your project. You can include it directly in the app heador directly in the resource (e.g. Article) like so:
`
To further enable this, you will have to enable SanitizeHelper module which provides a set of methods for scrubbing text of undesired HTML elements. These helper methods extend Action View making them callable within your template files.
In your project root directory, run touch config/initializers/html_sanitization.rb and add the following methods: