写真投稿アプリを作成しているのですが、新規作成のさい、名前だけを入力するとフラッシュメッセージでエラーが出て欲しいのですが、下記のエラーが出てしまいます。
解決法を教えていただければ幸いです。
error
1ActiveRecord::NotNullViolation in PhotosController#create 2PG::NotNullViolation: ERROR: null value in column "image" violates not-null constraint DETAIL: Failing row contains (35, null, a, , , , , , 3, 1, 2020-09-21 07:41:26.677697, 2020-09-21 07:41:26.677697). : INSERT INTO "photos" ("name", "nated_food", "dislike_word", "hate_human", "weaknes", "remark", "group_id", "user_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) RETURNING "id" 3Extracted source (around line #16): 414 515 616 717 818 919 10 11 def create 12 @photo = @group.photos.new(photo_params) 13 if @photo.save 14 redirect_to group_photos_path(@group), notice: 'my truthを作成しました' 15 else 16 @photos = @group.photos.includes(:user) 17 18Rails.root: /Users/user/Desktop/my-truth
controller
1class PhotosController < ApplicationController 2 before_action :set_group 3 before_action :set_photos 4 5 def index 6 @groups = Group.new 7 @photos = @group.photos.includes(:user) 8 end 9 10 def new 11 @photo = Photo.new 12 end 13 14 def create 15 @photo = @group.photos.new(photo_params) 16 if @photo.save 17 redirect_to group_photos_path(@group), notice: 'my truthを作成しました' 18 else 19 @photos = @group.photos.includes(:user) 20 flash.now[:alert] = '入力してください。' 21 render :new 22 end 23 end 24 25 def show 26 @photo = Photo.find(params[:id]) 27 end 28 29 private 30 def photo_params 31 params.require(:photo).permit(:name,:image,:nated_food,:dislike_word,:hate_human,:weaknes,:remark).merge(user_id: current_user.id) 32 end 33 34 def set_group 35 @group = Group.find(params[:group_id]) 36 end 37 38 def set_photos 39 @photos = Photo.all 40 end 41 42end
haml
1.wrapper 2 = render "groups/grouptop" 3 .container 4 = form_for [@group,@photo] do |form| 5 .form_center 6 %p1 新規投稿 7 %br 8 %a ニックネーム: 9 %a{class: "condition"}必須 10 %br 11 = form.text_area :name, class: "text_space",placeholder: "ニックネーム" 12 13 %br 14 %a 変顔写真: 15 %a{class: "condition"}必須 16 %br 17 = form.file_field :image, class: "text_space",placeholder: "Image Url" 18 19 %br 20 %a 嫌いな食べ物: 21 %a{class: "condition"}必須 22 %br 23 = form.text_area :nated_food, class: "text_space",placeholder: "例:ニンジン" 24 25 %br 26 %a 嫌いな言葉: 27 %a{class: "condition"}必須 28 %br 29 = form.text_area :dislike_word, class: "text_space",placeholder: "例:あほ" 30 31 %br 32 %a 嫌いな人間: 33 %a{class: "condition"}必須 34 %br 35 = form.text_area :hate_human, class: "text_space",placeholder: "例:ケチなやつ" 36 37 %br 38 %a 弱点: 39 %a{class: "condition"}必須 40 %br 41 = form.text_area :weaknes, class: "text_space",placeholder: "例:脇腹" 42 43 %br 44 %a コメント: 45 %a{class: "condition"}必須 46 %br 47 = form.text_area :remark, class: "text_space",placeholder: "コメント" 48 49 %br 50 %br 51 = form.submit "SEND",class: "send_btn"
migrate
1class CreatePhotos < ActiveRecord::Migration[5.2] 2 def change 3 create_table :photos do |t| 4 t.text :image,null: false 5 t.string :name,null: false 6 t.text :nated_food,null: false 7 t.text :dislike_word,null: false 8 t.text :hate_human,null: false 9 t.text :weaknes,null: false 10 t.text :remark,null: false 11 t.references :group, foreign_key: true 12 t.references :user, foreign_key: true 13 t.timestamps 14 end 15 end 16end 17
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/09/21 16:40
2020/09/23 02:10
2020/09/23 02:24