0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Active Storageで動画投稿

Posted at

★Active Storageは画像、動画が投稿できる

Active Storageで動画の投稿している記事が少なかったので
carrier waveは使わず、こっちでチャレンジしました!!

❶バリデーションに形式を記述

validates:videoの部分は現状presenseが適用されていない。。
video_type→携帯撮影をする程で作成しているためtypeで指定
video_size→10秒で20MB程の為、一応指定

#app/models/post.rb
class Post < ApplicationRecord
#省略
  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, '動画は携帯で撮影したmov形式でアップロードしてください')
    end
  end

  def video_size
    if video.blob.byte_size > 20.megabytes
      errors.add(:video, "動画を短く撮影し直してください(20MB以内)")
    end
  end

end

❷ビューファイルにvideoタグを記述

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

★Active Storageは便利…

今回の教科書はこちら

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?