[Ruby] Post videos with rails

1 minute read

This time I will post for the first time. Please understand that it may be difficult to see.

This time, it is a memorandum of video upload function with rails. I personally found it difficult to understand how to upload videos using FFmpeg, so I hope I can explain as easily as possible.

environment

Ruby 2.6.5 Rails 6.0.3.2

Get started immediately

terminal


rails new RailsApp

Create RailsApp in the terminal.

Gemfile


gem'carrierwave'
gem'mini_magick'

Add to Gemfile

terminal


rails bundle install

Do bundle install in the terminal.

terminal


rails g uploader video
rails g scaffold post video:string

Create uploader and scaffold in terminal

terminal


rails db:migrate

Don’t forget to migrate

app/models/post.rb


mount_uploader :video, VideoUploader

Added to post.rb

app/uploaders/video_uploader.rb


def extension_whitelist
   %w(jpg jpeg gif png MOV wmv mp4)
end

Remove comment from line 38 and add

ruby:views/posts/_form.html.erb


<div class="field">
    <%= form.label :video %>
    <%= form.file_field :video, :accept =>'video/*' %>
</div>

I think that it is text.field, so rewrite it as file.field.

ruby:app/views/posts/show.html.erb


<p>
  <%= link_to @post.video_url.to_s do %>
   <%= video_tag(@post.video.to_s) %>
  <% end %>
</p>

I think it is <%= @post.video %> so I will rewrite it. If you do not write <%= link_to @post.video_url.to_s do %>, it will not play even if you click the video.

Thank you very much

I’ve had a hard time uploading videos, so I hope you guys made it easy.