今某フリマアプリのクローンサイトを作っているのですが写真を複数枚投稿したいのですがうまくデータベースに保存することができません。
今の状態をとしてはProductが親モデルでproduct_image子モデルです。
[1] pry(#<ProductsController>)> params => <ActionController::Parameters {"utf8"=>"✓", "authenticity_token"=>"kyCkQIQm9Ze+EUoDs9+vU3n+ceZ4iB+zwZ8K3leUmv7YmfGN5pAVMHp0XuWBNBnLfZTP1Y5VLxVFaYvMW4Rc5Q==", "product"=><ActionController::Parameters {"product_images_attributes"=>{"0"=>{"image"=>[#<ActionDispatch::Http::UploadedFile:0x00007fc7698d9f68 @tempfile=#<Tempfile:/var/folders/5k/4m94gmtd0s153tdmqgr9gmth0000gn/T/RackMultipart20190303-4946-1cn6u8u.png>, @original_filename="phone.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"product[product_images_attributes][0][image][]\"; filename=\"phone.png\"\r\nContent-Type: image/png\r\n">, #<ActionDispatch::Http::UploadedFile:0x00007fc7698d9f18 @tempfile=#<Tempfile:/var/folders/5k/4m94gmtd0s153tdmqgr9gmth0000gn/T/RackMultipart20190303-4946-w3y2jr.png>, @original_filename="phone.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"product[product_images_attributes][0][image][]\"; filename=\"phone.png\"\r\nContent-Type: image/png\r\n">, #<ActionDispatch::Http::UploadedFile:0x00007fc7698d9ec8 @tempfile=#<Tempfile:/var/folders/5k/4m94gmtd0s153tdmqgr9gmth0000gn/T/RackMultipart20190303-4946-77rqyp.jpg>, @original_filename="phone.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"product[product_images_attributes][0][image][]\"; filename=\"phone.jpg\"\r\nContent-Type: image/jpeg\r\n">]}}, "name"=>"sasa", "description"=>"sasa", "category_id"=>"107", "size"=>"M", "brand"=>"", "product_status"=>"未使用に近い", "delivery_fee"=>"着払い", "local"=>"岩手", "lead_time"=>"2~3日で発送", "price"=>"300", "transaction_status"=>"出品中"} permitted: false>, "controller"=>"products", "action"=>"create"} permitted: false> [2] pry(#<ProductsController>)> product => #<Product:0x00007fc76989a340 id: 49, name: "sasa", description: "sasa", price: 300, likes_count: nil, size: "M", product_status: "未使用に近い", transaction_status: "出品中", delivery_fee: "着払い", local: "岩手", lead_time: "2~3日で発送", brand: nil, category_id: 107, created_at: Sun, 03 Mar 2019 13:32:32 UTC +00:00, updated_at: Sun, 03 Mar 2019 13:32:32 UTC +00:00> [3] pry(#<ProductsController>)> product.product_images ProductImage Load (0.3ms) SELECT `product_images`.* FROM `product_images` WHERE `product_images`.`product_id` = 49 => [#<ProductImage:0x00007fc76229fd48 id: 10, image: "[\"phone.png\", \"phone.png\", \"phone.jpg\"]", product_id: 49, created_at: Sun, 03 Mar 2019 13:32:32 UTC +00:00, updated_at: Sun, 03 Mar 2019 13:32:32 UTC +00:00>] [4] pry(#<ProductsController>)>
パラメータはこんな感じです。
product_controller
class ProductsController < ApplicationController def new @product = Product.new @product.product_images.build render layout: 'sell-form' end def middlecategory @middlecategory = Category.find(params[:category_id]).children respond_to do |form| # form.html{redirect_to new_product_path} form.json{@middlecategory} end end def smallcategory end def create product = Product.create(product_parameter) binding.pry redirect_to root_path end private def product_parameter params.require(:product).permit(:name, :description, :category_id, :size, :product_status, :delivery_fee, :local, :lead_time, :price, :transaction_status, product_images_attributes: {image: []}) end end
渡ってくるproduct_imageのimageをそれぞれちがうインスタンスとして保存したいのですが同じインスタンスの中に保存されてしまっているようです。
解決方法を教えてくださると嬉しいです。
あなたの回答
tips
プレビュー