You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Rails Active Storage video_tag无法显示视频问题求助

Fixing Video Playback Issues with Active Storage in Rails 5.2.3

Hey there, let's work through this video playback issue you're facing with Active Storage in Rails 5.2.3. It's super frustrating when uploads work but playback fails, so let's break down the most likely fixes step by step!

1. First, Check the Blob's Content-Type in the Database

The most common culprit here is that Active Storage stored the wrong content type for your video blob. If the content_type is set to text/html instead of video/mp4, browsers will never recognize it as a valid video file.

  • Open your Rails console with rails c
  • Run this to find your video blob and check its content type:
    # Replace "your-video-filename.mp4" with actual filename
    blob = ActiveStorage::Blob.find_by(filename: "your-video-filename.mp4")
    puts blob.content_type
    
  • If the output is anything other than video/mp4, update it immediately:
    blob.update(content_type: "video/mp4")
    
  • Now restart your server and test playback again.

2. Fix MIME Type Detection for Future Uploads

If the content type is being misdetected on upload, we can ensure Active Storage uses the correct type either by forcing it or improving detection:

Option A: Force the Content Type in Your Model

Add a callback to your model to set the correct content type before saving:

class YourModel < ApplicationRecord
  has_one_attached :video

  before_save :ensure_video_content_type

  private

  def ensure_video_content_type
    return unless video.attached?

    # Only update if the detected type is wrong
    unless video.content_type.in? ["video/mp4", "video/mov", "video/webm"]
      video.blob.update(content_type: "video/mp4")
    end
  end
end

Option B: Verify Marcel is Working Correctly

Rails uses the marcel gem to detect MIME types. For Rails 5.2.3, ensure your Gemfile includes a compatible version (it should be there by default, but double-check):

gem 'marcel', '~> 0.3.3' # Compatible with Rails 5.2.x

Run bundle install and restart your server to make sure it's loaded properly.

3. Clean Up Your MIME Type Initializer

The line you added to config/initializers/mime_types.rb might be redundant or conflicting with Rails' default MIME types:

# Remove this line if you added it, Rails already includes video/mp4 by default
# Rack::Mime::MIME_TYPES.merge!({ ".mp4" => "video/mp4" })

Rails has built-in support for video MIME types, so manually overriding them can sometimes cause unexpected issues. Delete that line, restart your server, and test again.

4. Double-Check Your Video Tag Implementation

Make sure you're generating the correct URL for the video file using Active Storage's helpers:

Using Rails' video_tag Helper

This is the simplest and most reliable way:

<%= video_tag @your_model.video, controls: true, width: "600" %>

Rails will automatically handle the correct src URL and type attribute for you.

Using Native HTML Video Tag

If you prefer native HTML, ensure you're using url_for to get the proper Active Storage service URL:

<video controls width="600">
  <source src="<%= url_for(@your_model.video) %>" type="video/mp4">
  Your browser doesn't support HTML5 video.
</video>

Avoid hardcoding file paths—Active Storage's service URLs are designed to handle content type headers correctly.

5. Verify Server Configuration (For Production)

If you're testing in production (not just development), make sure your web server (Nginx/Apache) is configured to serve video files with the correct MIME types:

  • For Nginx, add or verify this in your server config:
    types {
      video/mp4 mp4;
      video/mov mov;
      video/webm webm;
    }
    
  • For Apache, ensure mod_mime is enabled and add:
    AddType video/mp4 .mp4
    AddType video/mov .mov
    AddType video/webm .webm
    

内容的提问来源于stack exchange,提问作者pride

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 07:36:23