前提・実現したいこと
ご覧頂きありがとうございます。
現在Ruby on Railsで動画を投稿し共有するアプリを作成しております。
ところが、createアクションを起こすと必ず以下のエラーが現れデータベースに保存されません。
ActiveRecord::RecordInvalid: Validation failed: Video can't be blank
現在の仕様はSessionを用いたウィザード形式で実装を進めており、createアクションでbinding.pryを設定しparamsも確認したのですが、paramsにはデータが渡っていました。
なにとぞご助力のほどお願い申し上げます。
#発生しているエラーメッセージや問題
terminal
1[1] pry(#<PerformStepsController>)> @performance.save 2=> false 3[2] pry(#<PerformStepsController>)> @performance.errors.full_messages 4=> ["Video can't be blank"] 5
該当のソースコード
migration
1class CreatePerformances < ActiveRecord::Migration[6.0] 2 def change 3 create_table :performances do |t| 4 t.string :title, null: false 5 t.text :explain, null:false 6 t.date :start_date, null:false 7 t.date :finish_date, null:false 8 t.string :video 9 t.text :time_table, null: false 10 t.integer :price, null: false 11 t.string :place, null:false 12 t.text :staff, null:false 13 t.text :player, null:false 14 t.string :writer, null:false 15 t.string :directer, null: false 16 t.integer :audience, null: false 17 t.integer :rest, null: false 18 t.text :other_notice 19 t.integer :play_hour, null: false 20 t.integer :play_minutes, null: false 21 22 23 t.references :group ,null: false, foreign_key: true 24 25 26 t.timestamps 27 end 28 end 29end 30
controller
1 2class PerformStepsController < ApplicationController 3 def index 4 end 5 6 def step1 7 @performance = Performance.new 8 end 9 10 11 def step1_validates 12 @performance = set_perform_info(performance_params) 13 @performance.valid? 14 15 skip_company_validate(@performance.errors) 16 if @performance.errors.messages.blank? && @performance.errors.details.blank? 17 create_session(performance_params) 18 redirect_to step2_perform_steps_path and return 19 else 20 render :step1 21 end 22 end 23 24 def step2 25 @performance = Performance.new 26 end 27 28 def create 29 set_perform_with_session 30 @performance[:video] = performance_params[:video] 31 @performance[:player] = performance_params[:player] 32 @performance[:staff] = performance_params[:staff] 33 @performance[:writer] = performance_params[:writer] 34 @performance[:directer] = performance_params[:directer] 35 @performance[:audience] = performance_params[:audience] 36 @performance[:rest] = performance_params[:rest] 37 @performance[:place] = performance_params[:place] 38 @performance[:play_hour] = performance_params[:play_hour] 39 @performance[:play_minutes] = performance_params[:play_minutes] 40 @performance[:other_notice] = performance_params[:other_notice] 41 @performance[:group_id] = performance_params[:group_id] 42 binding.pry 43 44 if @performance.save 45 delete_session 46 redirect_to root_path 47 else 48 render :step2 49 end 50 end 51 52 private 53 54 def performance_params 55 params.require(:performance).permit(:title, 56 :explain, 57 :start_date, 58 :finish_date, 59 :image, 60 :video, 61 :time_table, 62 :price, 63 :player, 64 :staff, 65 :place, 66 :writer, 67 :directer, 68 :play_hour, 69 :play_minutes, 70 :audience, 71 :rest, 72 :other_notice 73 ).merge(group_id: current_group.id) 74 end 75 76 77 def set_perform_info(performance_params) 78 Performance.new( 79 title: performance_params[:title], 80 explain: performance_params[:explain], 81 start_date: performance_params[:start_date], 82 finish_date: performance_params[:finish_date], 83 image: performance_params[:image], 84 video: performance_params[:video], 85 time_table: performance_params[:time_table], 86 price: performance_params[:price], 87 player: performance_params[:player], 88 staff: performance_params[:staff], 89 writer: performance_params[:writer], 90 directer: performance_params[:directer], 91 play_hour: performance_params[:play_hour], 92 play_minutes: performance_params[:play_minutes], 93 audience: performance_params[:audience], 94 rest: performance_params[:rest], 95 other_notice: performance_params[:other_notice], 96 group_id: performance_params[:group_id] 97 ) 98 end 99 100 def set_perform_with_session 101 binding.pry 102 @performance = Performance.new( 103 title: session[:title], 104 explain: session[:explain], 105 start_date: session[:start_date], 106 finish_date: session[:finish_date], 107 time_table: session[:time_table], 108 price: session[:price], 109 image: session[:image], 110 video: session[:video], 111 player: session[:player], 112 staff: session[:staff], 113 writer: session[:writer], 114 directer: session[:directer], 115 place: session[:place], 116 play_hour: session[:play_hour], 117 play_minutes: session[:play_minutes], 118 audience: session[:audience], 119 rest: session[:rest], 120 other_notice: session[:other_notice], 121 group_id: session[:group_id] 122 ) 123 end 124 125 def skip_company_validate(errors) 126 errors.messages.delete(:video) 127 errors.details.delete(:video) 128 errors.messages.delete(:player) 129 errors.details.delete(:player) 130 errors.messages.delete(:staff) 131 errors.details.delete(:staff) 132 errors.messages.delete(:writer) 133 errors.details.delete(:writer) 134 errors.messages.delete(:directer) 135 errors.details.delete(:directer) 136 errors.messages.delete(:play_hour) 137 errors.details.delete(:play_hour) 138 errors.messages.delete(:play_minutes) 139 errors.details.delete(:play_minutes) 140 errors.messages.delete(:place) 141 errors.details.delete(:place) 142 errors.messages.delete(:audience) 143 errors.details.delete(:audience) 144 errors.messages.delete(:rest) 145 errors.details.delete(:rest) 146 errors.messages.delete(:other_notice) 147 errors.details.delete(:other_notice) 148 end 149 150 def create_session(performance_params) 151 session[:title] = performance_params[:title] 152 session[:explain] = performance_params[:explain] 153 session[:start_date] = performance_params[:start_date] 154 session[:finish_date] = performance_params[:finish_date] 155 session[:time_table] = performance_params[:time_table] 156 session[:price] = performance_params[:price] 157 session[:image] = performance_params[:image] 158 session[:video] = performance_params[:video] 159 session[:player] = performance_params[:player] 160 session[:staff] = performance_params[:staff] 161 session[:writer] = performance_params[:writer] 162 session[:directer] = performance_params[:directer] 163 session[:place] = performance_params[:place] 164 session[:play_hour] = performance_params[:play_hour] 165 session[:play_minutes] = performance_params[:play_minutes] 166 session[:audience] = performance_params[:audience] 167 session[:rest] = performance_params[:rest] 168 session[:other_notice] = performance_params[:other_notice] 169 session[:group_id] = current_group.id 170 end 171 172 def delete_session 173 session.delete(:title) 174 session.delete(:explain) 175 session.delete(:start_date) 176 session.delete(:finish_date) 177 session.delete(:time_table) 178 session.delete(:price) 179 session.delete(:video) 180 session.delete(:player) 181 session.delete(:staff) 182 session.delete(:writer) 183 session.delete(:directer) 184 session.delete(:place) 185 session.delete(:play_hour) 186 session.delete(:play_minutes) 187 session.delete(:audience) 188 session.delete(:rest) 189 session.delete(:other_notice) 190 end 191end
params
1[6] pry(#<PerformStepsController>)> @performance 2=> #<Performance:0x00007fc3be13ae50 3〜省略〜 4 video: "#<ActionDispatch::Http::UploadedFile:0x00007fc3aa9a45c8>", 5〜省略〜
@performance_params[:video]の中身
pry(#<PerformStepsController>)> performance_params[:video] => #<ActionDispatch::Http::UploadedFile:0x00007fc3aa9a45c8 @content_type="video/quicktime", @headers="Content-Disposition: form-data; name=\"performance[video]\"; filename=\"ANCHOR 2nd.MOV\"\r\nContent-Type: video/quicktime\r\n", @original_filename="ANCHOR 2nd.MOV", @tempfile=#<File:/var/folders/qm/bz6p8g9d5l985h9hcsp0bgh00000gn/T/RackMultipart20210524-30005-1ctz62y.MOV>>
試したこと
ビューファイルのフォームにmultipart:true
を引数に組み込んだり、binding.pry
でvideoの挙動を逐一確認したりしていました。
マイグレーションのvideoカラムもnull: false
の記述も削除しております。
他にもTypeError (hash key "tempfile" is not a Symbol) carriewave
やActiveRecord::NotNullViolation
等のエラーにも遭遇しましたが、気づいたら今回のエラーにたどり着きました。
もしかしたら今回のエラー以前に解決すべき問題があるのかもしれませんが、初学者でまだ右も左もわからない状態でございます。
改めてどうぞよろしくお願い申し上げます。
参考にしたサイト
devise+ウィザード形式でのユーザ登録←この方の投稿を基に作成しております。
ActiveRecord::NotNullViolation が突破できません。
Rails 画像アップロード機能の実装方法 メモ
Rails 画像表示
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。