[RUBY] Post videos with Active Storage

★ Active Storage allows you to post images and videos

Since there were few articles posting videos on Active Storage I challenged here without using carrier wave! !!

❶ Describe the format in the validation

Currently, presense is not applied to the validates: video part. .. video_type → Specify by type because it is created enough to shoot with a mobile phone video_size → About 20MB in 10 seconds, so specify for the time being

#app/models/post.rb
class Post < ApplicationRecord
#abridgement
  with_options presence: true do
    validates :title
    validates :price, format: { with: /\A[-]?[0-9]+(\.[0-9]+)?\z/}
    validates_inclusion_of :price, in: 500..5000
    validates :video
  end
  validate :video_type
  validate :video_size

  private

  def video_type
    if !video.blob.content_type.in?(%('video/quicktime video/quicktime'))
        errors.add(:video, 'Please upload the video in mov format taken with your mobile phone')
    end
  end

  def video_size
    if video.blob.byte_size > 20.megabytes
      errors.add(:video, "Please retake the video short(Within 20MB)")
    end
  end

end

❷ Write a video tag in the view file

#app/views/posts/index.html.erb
class PostsController < ApplicationController
#abridgement
<video src=<%= rails_blob_path(post.video) %> type="video/mov", controls></video>

★ Active Storage is convenient ...

Click here for this textbook

Recommended Posts

Post videos with Active Storage
Post / delete multiple images with Active Storage
Introduce Active Storage
About Active Storage
[Beginner] About Active Storage
[Implementation procedure] Implement image upload function with Active Storage
[Rails] Upload videos with Rails (ActiveStorage)
Test Active Strage with RSpec
Post an image with POSTMAN
Move Active Storage on S3 locally
[Rails] How to use Active Storage
Unit tests under Active Storage deployment