"product"という親レコードを保存した際、同時に"item_record"という1以上の複数の子レコードをDBに保存する仕様を実装したいです。
accepts_nested_attributes_forとinverse_ofを使って実装を試みておりますが、productデータ保存時にparamsでどうやってitem_imageの情報を持たせたらいいかがわかっておりません。
この点について、どうすれば綺麗にデータがDBに保存されるか、ご教授頂きたいです。
現在のエラー文(products/newを表示した時)
/Users/miwataiga/projects/freemarket_sample_43a/app/controllers/products_controller.rb:19: syntax error, unexpected '}', expecting => ...tem_image_attributes: { :name }) ... ^
app/controllers/products_controller.rb
ruby
1class ProductsController < ApplicationController 2 3 def new 4 @product = Product.new 5 end 6 7 def create 8 @product = Product.new(product_params) 9 binding.pry 10 if @product.save 11 redirect_to root_path, notice: '出品しました。' 12 else 13 render :new 14 end 15 end 16 17 private 18 def product_params 19 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_image_attributes: { :name }) 20 end 21end
app/views/products/new.html.hamlの該当部分
ruby
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 = form_for :item_images, @product.item_image.build do |c| 15 = c.file_field :name, class: 'image-upload-dropfile hidden', type: 'file' 16 %pre.visible-pc 17 ドラッグアンドドロップ<br />またはクリックしてファイルをアップロード 18 %i.icon-camera
app/views/models/product.rb
ruby
1class Product < ApplicationRecord 2 has_many :item_images, inverse_of: :item_image 3 accepts_nested_attributes_for :item_images, allow_destroy: true 4end
app/views/models/item_image.rb
ruby
1class ItemImage < ApplicationRecord 2 mount_uploader :name, ImageUploader 3 belongs_to :product, inverse_of: :product 4end
追記
何とか画像はDB保存出来るようになったのですが、今度は1つの画像ファイルしか保存できませんでした。
app/controllers/products_controller.rb
#修正前 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_image_attributes: { :name }) #修正後 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])
app/views/products/new.html.hamlの該当部分
/ 修正前 = form_for @product, html: {class: "new-main__sell"} do |f| .new-main__image %h3.new-main__image-head 出品画像 %span.form-require 必須 %p 最大10枚までアップロードできます .image-upload-dropbox__container.clearfix.state-image-number-10 .image-upload-items__container .image-upload-items.have-item-0 %ul %label.image-upload-dropbox.have-item-0 = form_for :item_images, @product.item_image.build do |c| = c.file_field :name, class: 'image-upload-dropfile hidden', type: 'file' %pre.visible-pc ドラッグアンドドロップ<br />またはクリックしてファイルをアップロード %i.icon-camera / 修正後 = form_for @product, html: {class: "new-main__sell"} do |f| .new-main__image %h3.new-main__image-head 出品画像 %span.form-require 必須 %p 最大10枚までアップロードできます .image-upload-dropbox__container.clearfix.state-image-number-10 .image-upload-items__container .image-upload-items.have-item-0 %ul %label.image-upload-dropbox.have-item-0 = f.fields_for :item_images do |i| = i.file_field :name, class: 'image-upload-dropfile hidden', type: 'file' %pre.visible-pc ドラッグアンドドロップ<br />またはクリックしてファイルをアップロード %i.icon-camera
そこで、i.file_fieldにmultiple: trueの記述を行いましたが、今度はparamsには選択した画像データ全て格納されているものの、@productの中には画像データが入らなくなってしまいました。
この点について、どのように解決すれば良いでしょうか?
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/02/05 05:20
2019/02/06 05:47