
事象
2つのモデルに同時登録をしたいため、accepts_nested_attributes_forを利用しているんですが、
strong parameterのparamsに渡したデータを取り出せず困っております。
約丸1日時間を使って格闘しておりますが解決出来ないので、
こちらで質問させて頂きました。
環境
Rails 5.0.7.1
Model
モデルはproductとitem_imageの2つを使用しております。
Ruby
1app/models/product.rb 2 3class Product < ApplicationRecord 4 has_many :item_images, :dependent => :destroy 5 accepts_nested_attributes_for :item_images, allow_destroy: true 6end 7 8app/models/item_image.rb 9 10class ItemImage < ApplicationRecord 11 mount_uploader :name, ImageUploader 12 belongs_to :product, optional: true 13end
Contoroller
コントローラーではproductを中心にネストさせたitem_imageを保存する形です。
ruby
1class ProductsController < ApplicationController 2 3 def new 4 @product = Product.new 5 @product.item_images.build 6 end 7 8 def create 9 binding.pry 10 @product = Product.new(product_params) 11 if @product.save 12 redirect_to root_path, notice: '出品しました。' 13 else 14 render :new 15 end 16 end 17 18 private 19 def product_params 20 params.require(:product).permit(:name, :description, :category_large, :category_middle, :category_small, :brand, :size, :shipping_charges_burden, :dispatch_area, :shipping_method, :number_of_the_days_to_ship, :price, :condition, item_images_attributes: [:name]) 21 end 22end
View
haml
1= form_for @product, html: {class: "new-main__sell"} do |f| 2 .new-main__image 3 %h3.new-main__image-head 4 出品画像 5 %span.form-require 6 必須 7 %p 8 最大10枚までアップロードできます 9 .image-upload-dropbox__container.clearfix.state-image-number-10 10 .image-upload-items__container 11 .image-upload-items.have-item-0 12 %ul 13 %label.image-upload-dropbox.have-item-0 14 = f.fields_for :item_images do |i| 15 = i.file_field :name, multiple: true, class: 'image-upload-dropfile hidden', type: 'file' 16 %pre.visible-pc 17 ドラッグアンドドロップ<br />またはクリックしてファイルをアップロード 18 %i.icon-camera
paramsの中身
ファイル名"1.png", "2.png", "3.png"をアップロードした時のparamsの中身です。
<ActionController::Parameters {"utf8"=>"✓", "authenticity_token"=>"UrXgrCUx9A7MeBZ6XBIOGr9dPB+DFl3TEFyCItecugR9VUwMpJICHVJW2kKkPZqoEQOM9o5jrxCj6mU/zdL+yQ==", "product"=>{"item_images_attributes"=>{"0"=>{"name"=>[#<ActionDispatch::Http::UploadedFile:0x007fcdb3cd8a60 @tempfile=#Tempfile:/var/folders/8w/0jszx71s4999fhh3qryy0lyh0000gn/T/RackMultipart20190206-8800-1vlw5r9.png, @original_filename="3.png", @content_type="image/png", @headers="Content-Disposition: form-data; name="product[item_images_attributes][0][name][]"; filename="3.png"\r\nContent-Type: image/png\r\n">, #<ActionDispatch::Http::UploadedFile:0x007fcdb3cd8a10 @tempfile=#Tempfile:/var/folders/8w/0jszx71s4999fhh3qryy0lyh0000gn/T/RackMultipart20190206-8800-1h3qyj3.png, @original_filename="2.png", @content_type="image/png", @headers="Content-Disposition: form-data; name="product[item_images_attributes][0][name][]"; filename="2.png"\r\nContent-Type: image/png\r\n">, #<ActionDispatch::Http::UploadedFile:0x007fcdb3cd89c0 @tempfile=#Tempfile:/var/folders/8w/0jszx71s4999fhh3qryy0lyh0000gn/T/RackMultipart20190206-8800-zi8ilf.png, @original_filename="1.png", @content_type="image/png", @headers="Content-Disposition: form-data; name="product[item_images_attributes][0][name][]"; filename="1.png"\r\nContent-Type: image/png\r\n">]}}, "name"=>"", "description"=>"", "category_large"=>"---", "category_middle"=>"---", "category_small"=>"---", "size"=>"---", "brand"=>"", "condition"=>"---", "shipping_charges_burden"=>"---", "shipping_method"=>"---", "dispatch_area"=>"---", "number_of_the_days_to_ship"=>"---", "price"=>"1000"}, "button"=>"", "controller"=>"products", "action"=>"create"} permitted: false>
困っていること
paramsにはitem_imageの情報が渡されているのに、そこから取り出すことが出来ず、結果的にDBにはproductテーブルに1レコード保存され、item_imageテーブルにもnameカラムが空の状態で1レコードだけ保存されるような状態です。
どうすればproduct 1レコードに対してitem_image が正しく複数レコード保存されるようになるでしょうか?
アドバイスを頂きたいです。
追記
paramsから値が取り出せないとはどういうことか、というご質問を頂きましたので、コンソールの画像を添付します。
追記#2
Image_itemモデルのnameカラムにバリデーションをかけたところ、コンソールに下記が表示されました。
やはりparamsの値がpermitされていないようです。
回答3件
あなたの回答
tips
プレビュー