This is my first post. Please note that it may be a little difficult to see.
This time, it is a memorandum of the video upload function in rails. Personally, uploading videos using FFmpeg was difficult to understand, so I would like to explain it in an easy-to-understand manner.
Ruby 2.6.5 Rails 6.0.3.2
Terminal
rails new RailsApp
Create a Rails App in the terminal.
Gemfile
gem 'carrierwave'
gem 'mini_magick'
Added to Gemfile
Terminal
rails bundle install
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 the comment from the 38th line and add it
ruby:views/posts/_form.html.erb
<div class="field">
<%= form.label :video %>
<%= form.file_field :video, :accept => 'video/*' %>
</div>
I think it is text.field, so rewrite it to 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 rewrite it. If you do not write <% = link_to @ post.video_url.to_s do%>, the video will not play even if you click it.
I struggled to upload the video, so I hope you can do it easily.
Recommended Posts