CarrierWaveを利用して動画投稿アプリの作成しております。
投稿一覧を表示しようとしたときに表記のエラーが発生しました。
■■な機能を実装中に以下のエラーメッセージが発生しました。
発生している問題・エラーメッセージ
該当のソースコード
index.html.slim
- @posts.each do |post| p = post.caption = video_tag post.videos.first.video.url(:medium)
new.html.slim
= form_with model: @post do |f| = f.label :caption = f.text_field :caption = f.fields_for :videos do |i| = i.file_field :video = f.submit "投稿", class: "btn btn-primary"
post.controller.rb
class PostsController < ApplicationController before_action :authenticate_user! 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 private def post_params params.require(:post).permit(:caption, videos_attributes: [:video]).merge(user_id: current_user.id) end end
post.rb
RUこ
1class Post < ApplicationRecord 2 belongs_to :user 3 has_many :videos, dependent: :destroy 4 5 mount_uploader :video, VideoUploader 6 7 accepts_nested_attributes_for :videos 8end 9
video.rb
class Video < ApplicationRecord belongs_to :post validates :video, presence: true end
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) # 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
試したこと
はじめはデータが保存出来てないのではと思いましたが、コンソールで確認するとVideoのデータは保存されていました。
補足情報(FW/ツールのバージョンなど)
助言・ご回答頂ければ幸いです。何卒よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/08/31 10:22