https://qiita.com/gakinchoy7/items/ac1d8e64e33c3ddd377b
https://qiita.com/0thefool/items/5af0f17f0075e7e0e013
上記のサイトを参考にして複数画像投稿機能を実装しました。
投稿自体は画像アリでもなしでも可能ですが
画像のない投稿に編集で画像を追加するとparamsは送られているのにnilが入ってしまいます。
解決法がわかる方は教えてください(__)。お願いします
Started PATCH "/posts/16" for ::1 at 2021-04-03 22:12:14 +0900 Processing by PostsController#update as HTML Parameters: {"authenticity_token"=>"[FILTERED]", "post"=>{"content"=>"こんにちわ", "images_attributes"=>{"0"=>{"image"=>"453789.jpg"}, "1"=>{"image"=>""}, "2"=>{"image"=>""}, "3"=>{"image"=>""}, "4"=>{"image"=>""}}, "tag_ids"=>"rails ruby"}, "commit"=>"投稿", "id"=>"16"} Post Load (0.1ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = $1 LIMIT $2 [["id", 16], ["LIMIT", 1]] ↳ app/controllers/posts_controller.rb:79:in `set_post' User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]] TRANSACTION (0.1ms) BEGIN ↳ app/controllers/posts_controller.rb:53:in `update' User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]] ↳ app/controllers/posts_controller.rb:53:in `update' Image Create (0.8ms) INSERT INTO "images" ("post_id", "image", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["post_id", 16], ["image", nil], ["created_at", "2021-04-03 13:12:14.156140"], ["updated_at", "2021-04-03 13:12:14.156140"]] ↳ app/controllers/posts_controller.rb:53:in `update' Image Create (0.1ms) INSERT INTO "images" ("post_id", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["post_id", 16], ["created_at", "2021-04-03 13:12:14.158176"], ["updated_at", "2021-04-03 13:12:14.158176"]] ↳ app/controllers/posts_controller.rb:53:in `update' Image Create (0.1ms) INSERT INTO "images" ("post_id", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["post_id", 16], ["created_at", "2021-04-03 13:12:14.159287"], ["updated_at", "2021-04-03 13:12:14.159287"]] ↳ app/controllers/posts_controller.rb:53:in `update' Image Create (0.3ms) INSERT INTO "images" ("post_id", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["post_id", 16], ["created_at", "2021-04-03 13:12:14.166773"], ["updated_at", "2021-04-03 13:12:14.166773"]] ↳ app/controllers/posts_controller.rb:53:in `update' Image Create (0.2ms) INSERT INTO "images" ("post_id", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["post_id", 16], ["created_at", "2021-04-03 13:12:14.168800"], ["updated_at", "2021-04-03 13:12:14.168800"]] ↳ app/controllers/posts_controller.rb:53:in `update' TRANSACTION (0.9ms) COMMIT ↳ app/controllers/posts_controller.rb:53:in `update' Redirected to http://localhost:3000/dashboard Completed 302 Found in 25ms (ActiveRecord: 3.0ms | Allocations: 13196)
edit
1 p 最大5枚までアップロードできます 2 .post__drop__box__container 3 .prev-content 4 5 //JSで挿入したhtmlと同じ形 each文でのプレビュー表示 6 - @post.images.each_with_index do |image,i| 7 .preview-box 8 .upper-box 9 = image_tag image.image.to_s, width: "112", height: "112", alt: "preview" 10 .lower-box 11 .update-box 12 .edit-btn 編集 13 .delete-box 14 div class="delete-btn" data-delete-id="#{i}" 削除 15 .label-content 16 = f.label :"images_attributes_#{@post.images.length}_image", class: "label-box" do 17 <i class="fas fa-camera"></i> 18 19 .hidden-content 20 = f.fields_for :images do |i| 21 22 //プレビューが出ている分のfile_fieldとdelete用のチェックボックスを設置 23 = i.file_field :image,class:"hidden-field" 24 = i.check_box:_destroy,class:"hidden-field" 25 26 //5つのfile_fieldを準備するに当たって、足りない分を表示 27 - @post.images.length.upto(4) do |i| 28 input name="post[images_attributes][#{i}][image]" id="post_images_attributes_#{i}_image" class="hidden-field" type="file" 29
あなたの回答
tips
プレビュー