CarrierWaveで動画を保存できない
ここに質問の内容を詳しく書いてください。
railsでCarrierWaveのgemを利用し動画投稿サイトを作成中です。
発生している問題・エラーメッセージ
CarrierWaveの実装が終わったのですが、画像はデータ格納、表示されるに対し、動画はデータベースに反映、viewに表示されない状態です。
該当のソースコード
video_uploader.rb
class VideoUploader < CarrierWave::Uploader::Base # Include RMagick or MiniMagick support: # include CarrierWave::RMagick include CarrierWave::MiniMagick # Choose what kind of storage to use for this uploader: storage :file # storage :fog # Override the directory where uploaded files will be stored. # This is a sensible default for uploaders that are meant to be mounted: def store_dir "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" end # Provide a default URL as a default if there hasn't been a file uploaded: # def default_url(*args) # # For Rails 3.1+ asset pipeline compatibility: # # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_')) # # "/images/fallback/" + [version_name, "default.png"].compact.join('_') # end # Process files as they are uploaded: # process scale: [200, 300] # # def scale(width, height) # # do something # end # Create different versions of your uploaded files: # version :thumb do # process resize_to_fit: [50, 50] # end version :medium do process resize_to_fill: [1080, 1080] end # Add a white list of extensions which are allowed to be uploaded. # For images you might use something like this: def extension_whitelist %w(jpg jpeg gif png mp4 mov wmv) end # Override the filename of the uploaded files: # Avoid using model.id or version_name here, see uploader/store.rb for details. # def filename # "something.jpg" if original_filename # end end
投稿画面
post/new.html.slim
.d-flex.flex-column.align-items-center.mt-3 .col-xl-7.col-lg-8.col-md-10.col-sm-11.post-card .card .card-header | 投稿画面 .card-body = form_with model: @post, class: "uplead-videos p-0 border-0" do |f| .form-group.row.mt-2 .col-auto.pr-0 = image_tag avatar_url(current_user), class: "post-profile-icon" .clo.pl-0 = f.text_field :caption, class: "form-control border-0", placeholder: "キャプションを書く" .mb-3 = f.fields_for :videos do |i| = i.file_field :video = f.submit "投稿する", class: "btn btn-primary"
以下model
video.rb
class Video < ApplicationRecord belongs_to :post mount_uploader :video, VideoUploader validates :video, presence: true end
post.rb
class Post < ApplicationRecord belongs_to :user has_many :videos, dependent: :destroy has_many :likes, dependent: :destroy has_many :comments, dependent: :destroy accepts_nested_attributes_for :videos def liked_by(user) Like.find_by(user_id: user.id, post_id: id) end end
コントローラー
posts.controller.rb
class PostsController < ApplicationController before_action :authenticate_user! before_action :set_post, only: %i(show destroy) def new @post = Post.new @post.videos.build end def create @post = Post.new(post_params) if @post.videos.present? @post.save redirect_to root_path flash[:notice] = "投稿が保存されました" else redirect_to root_path flash[:alert] = "投稿に失敗しました" end end def index @posts = Post.limit(10).includes(:videos, :user).order('created_at DESC') end def show # @post = Post.find_by(id: params[:id]) end def destroy # @post = Post.find_by(id: params[:id]) if @post.user == current_user flash[:notice] = "投稿が削除されました" if @post.destroy else flash[:alert] = "投稿の削除に失敗しました" end redirect_to root_path end private def post_params params.require(:post).permit(:caption, videos_attributes: [:video]).merge(user_id: current_user.id) end def set_post @post = Post.find_by(id: params[:id]) end end
その他状況
エラー画面は特に表示されておりません。(「投稿が保存されました」と出るため@post.saveはされている?)→コンソールで調べると動画のデータは保存されていない。画像は保存される。
画像はpubulic/uploads/videoに保存されてるのに対し、動画はpubulic/tmp内に保存されております。おそらくこの内容がおかしいのだと思うのですが今一歩解決に至ってない状況です。
お手数ですが、ご教授頂ければ幸いです。何卒よろしくお願いいたします。
あなたの回答
tips
プレビュー