#前提・実現したいこと
現在Rails でアプリを作成しています。
新しくお店を投稿しようとしたところ、以下のエラーメッセージが発生しました。
入力フォームを作成し、画像、店舗名、感想を入力してsubmitしたら,indexページへ飛ぶように設定したいです。
モデルはUser,Shopの2つです。
#発生している問題・エラーメッセージ
rails
1ActiveRecord::NotNullViolation in ShopsController#create 2 3SQLite3::ConstraintException: shops.image_id may not be NULL: INSERT INTO "shops" ("name", "review", "user_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?) 4 5Parameters: 6 7{"utf8"=>"✓", 8 "authenticity_token"=>"ZD+XgDGb6ZkV0xPq2SR+d4ukFgzD11dxsHnnYY/mDFIHDX3fhUQXJfQtMDOZ20imxRcSOHMkl5NW56NtGgJ8Pw==", 9 "shop"=> 10 {"image"=> 11 ["{}", 12 #<ActionDispatch::Http::UploadedFile:0x00007fed84855d08 13 @content_type="image/jpeg", 14 @headers="Content-Disposition: form-data; name=\"shop[image][]\"; filename=\"shokupan1.jpg\"\r\n" + "Content-Type: image/jpeg\r\n", 15 @original_filename="shokupan1.jpg", 16 @tempfile=#<File:/tmp/RackMultipart20211213-31017-3j4231.jpg>>, 17 #<ActionDispatch::Http::UploadedFile:0x00007fed84855ba0 18 @content_type="image/jpeg", 19 @headers="Content-Disposition: form-data; name=\"shop[image][]\"; filename=\"shokupan2.jpg\"\r\n" + "Content-Type: image/jpeg\r\n", 20 @original_filename="shokupan2.jpg", 21 @tempfile=#<File:/tmp/RackMultipart20211213-31017-15zxr7q.jpg>>], 22 "name"=>"食パン屋さん", 23 "review"=>"test"}, 24 "commit"=>"投稿"}
rails
1Started POST "/shops" for 180.147.217.168 at 2021-12-13 12:35:00 +0000 2Cannot render console from 180.147.217.168! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 3Processing by ShopsController#create as HTML 4 Parameters: {"utf8"=>"✓", "authenticity_token"=>"ZD+XgDGb6ZkV0xPq2SR+d4ukFgzD11dxsHnnYY/mDFIHDX3fhUQXJfQtMDOZ20imxRcSOHMkl5NW56NtGgJ8Pw==", "shop"=>{"image"=>["{}", #<ActionDispatch::Http::UploadedFile:0x00007fed84855d08 @tempfile=#<Tempfile:/tmp/RackMultipart20211213-31017-3j4231.jpg>, @original_filename="shokupan1.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"shop[image][]\"; filename=\"shokupan1.jpg\"\r\nContent-Type: image/jpeg\r\n">, #<ActionDispatch::Http::UploadedFile:0x00007fed84855ba0 @tempfile=#<Tempfile:/tmp/RackMultipart20211213-31017-15zxr7q.jpg>, @original_filename="shokupan2.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"shop[image][]\"; filename=\"shokupan2.jpg\"\r\nContent-Type: image/jpeg\r\n">], "name"=>"食パン屋さん", "review"=>"test"}, "commit"=>"投稿"}" 5 User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]] 6 ↳ /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/activerecord-5.2.6/lib/active_record/log_subscriber.rb:98 7Unpermitted parameter: :image 8 (0.1ms) begin transaction 9 ↳ app/controllers/shops_controller.rb:18 10 User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]] 11 ↳ app/controllers/shops_controller.rb:18 12 Shop Create (0.6ms) INSERT INTO "shops" ("name", "review", "user_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?) [["name", "食パン屋さん"], ["review", "test"], ["user_id", 1], ["created_at", "2021-12-13 12:35:00.533237"], ["updated_at", "2021-12-13 12:35:00.533237"]] 13 ↳ app/controllers/shops_controller.rb:18 14 (0.1ms) rollback transaction 15 ↳ app/controllers/shops_controller.rb:18 16Completed 500 Internal Server Error in 15ms (ActiveRecord: 1.0ms) 17 18 19 20ActiveRecord::NotNullViolation (SQLite3::ConstraintException: shops.image_id may not be NULL: INSERT INTO "shops" ("name", "review", "user_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?)): 21 22app/controllers/shops_controller.rb:18:in `create'
#該当のソースコード
/app/controllers/shops_controller.rb
Ruby
1class ShopsController < ApplicationController 2 before_action :authenticate_user! 3 4 5 6 def index 7 @shops = Shop.all 8 end 9 10 def new 11 @shop = Shop.new 12 end 13 14 def create 15 @shop = Shop.new(shop_params) 16 @shop.user_id = current_user.id 17 @shop.save 18 redirect_to shops_path 19 end 20 21 def show 22 @shop = Shop.find(params[:id]) 23 end 24 25 def destroy 26 end 27 28 private 29 30 def shop_params 31 params.require(:shop).permit(:image, :name, :review) 32 end 33end
/app/views/shops/new.html.erb
HTML
1<main> 2 <h2 class="title">NEW SHOP</h2> 3 <div class="container"> 4 <div class="row"> 5 <div class="col-lg-6 mx-auto"> 6 <h3>食パン 新規投稿フォーム</h3> 7 <p class="text-danger mb-5">※印は必須項目です。</p> 8 <%= form_with model: @shop, local:true do |f| %> 9 <div class="form-group"> 10 <%= f.label :image, "画像" %><span class="text-danger">※</span> 11 <%= f.attachment_field :image, multiple: true %> 12 </div> 13 <div class="form-group"> 14 <%= f.label :name, "店舗名" %><span class="text-danger">※</span> 15 <%= f.text_field :name, class: "form-control" %> 16 </div> 17 <div class="form-group"> 18 <%= f.label :review, "レビュー" %><span class="text-danger">※</span> 19 <%= f.text_area :review, rows: "5", class: "form-control" %> 20 </div> 21 <div class="my-5"> 22 <%= f.submit "投稿", class: "btn btn-outline-success mx-auto d-block" %> 23 </div> 24 <% end %> 25 </div> 26 </div> 27 </div> 28</main>
どうかご助力願います。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/12/13 13:32
2021/12/13 13:48
2021/12/13 14:46