質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

HTML5

HTML5 (Hyper Text Markup Language、バージョン 5)は、マークアップ言語であるHTMLの第5版です。

Q&A

0回答

2208閲覧

一つのフォームから複数のテーブルにデータを保存する方法

k-keisuke

総合スコア3

Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

HTML5

HTML5 (Hyper Text Markup Language、バージョン 5)は、マークアップ言語であるHTMLの第5版です。

0グッド

0クリップ

投稿2020/05/14 03:59

前提・実現したいこと

フリマアプリのコピーを作成しています。
出品機能を実装していて、一つのフォームから複数のテーブルにデータを保存したいと思っております。
form_withで@itemを親にして、fields_forで各テーブルを子としてフォームを作成し、ページ下部の”出品する”ボタンにて保存します。
以下のエラーが発生しますので、分かられる方がおりましたら、ご教示いただけますと幸いです。

発生している問題・エラーメッセージ

ActionController::ParameterMissing in ItemsController#create param is missing or the value is empty: brand Extracted source (around line #40): 38 39 40 41 42 43 def brand_params params.require(:brand).permit(:brand) end def item_image_params Rails.root: /Users/kandakeisuke/Desktop/team_development/freemarket_sample_74a Application Trace | Framework Trace | Full Trace app/controllers/items_controller.rb:40:in `brand_params' app/controllers/items_controller.rb:27:in `create' Request Parameters: {"utf8"=>"✓", "authenticity_token"=>"g7PZ/kFFP33vEdRNNCzccew0oYMfdZJpEmgJ4/acY1bTS2+CJ3Jroe1u6pm6m+QXo03k07tJVuGh2rlwXjShBA==", "item"=> {"item_image"=> {"image"=> #<ActionDispatch::Http::UploadedFile:0x00007f9e5a696ea0 @content_type="image/jpeg", @headers= "Content-Disposition: form-data; name=\"item[item_image][image]\"; filename=\"\xE9\xBA\xBB\xE5\xA9\x86\xE8\xB1\x86\xE8\x85\x90(\xE5\x86\x99\xE7\x9C\x9F).jpg\"\r\n" + "Content-Type: image/jpeg\r\n", @original_filename="麻婆豆腐(写真).jpg", @tempfile=#<File:/var/folders/0m/8sj_bl5x4vz9rch9148m5sqr0000gn/T/RackMultipart20200514-1469-bspnso.jpg>>}, "item_name"=>"kkkkk", "content"=>"っっっっっっっっl", "category_id"=>"8", "brand"=>{"brand"=>"dddddd"}, "status"=>"2", "shipping"=>{"ship_base"=>"2", "ship_date"=>"3"}, "price"=>"3000"}, "commit"=>"出品する"}

該当のソースコード

routes.rb

Rails.application.routes.draw do root 'items#index' resources :items, only: [:index, :show, :new, :create] resources :item_payment resources :brands, only: [:create] resources :item_images, only: [:create] resources :shippings, only: [:create] # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html end

items_controller.rb

class ItemsController < ApplicationController def index end def show end def new @item = Item.new @brand = Brand.new @item_image = ItemImage.new @shipping = Shipping.new end def create # @item = Item.new(item_params) # @item.save! # @brand = Brand.new(brand_params) # @brand.save! # @item_image = ItemImage.new(item_image_params) # @item_image.save! # @shipping = Shipping.new(shipping_params) # @shipping.save! # redirect_to root_path Item.create(item_params) Brand.create(brand_params) ItemImage.create(item_image_params) Shipping.create(shipping_params) redirect_to root_path end private def item_params params.require(:item).permit(:item_name, :price, :size, :content, :status) end def brand_params params.require(:brand).permit(:brand) end def item_image_params params.require(:item_image).permit(:image).merge(image_id: params[:image_id]) end def shipping_params params.require(:shipping).permit(:ship_base, :region, :city, :block, :ship_method, :ship_date).merge(item_id: params[:item_id]) end end

item.rb

class Item < ApplicationRecord validates :item_name, :price, :size, :content, :status, presence: true # belongs_to :user belongs_to :category belongs_to :brand has_one :shipping has_many :item_images end

brand.rb

class Brand < ApplicationRecord has_many :items end

item_image.rb

class ItemImage < ApplicationRecord belongs_to :item end

shipping.rb

class Shipping < ApplicationRecord belongs_to :item end
.title = link_to image_tag("logo.png", height: '49', width: '185', class: 'logo'), "/" .content .content-main = form_with model: @item, local: true do |f| .image-product %label.label-edit 出品画像 %sapn.required 必須 %p 最大10枚までアップロードできます。 .name__image .name__image__set = f.fields_for @item_image do |item_image| = item_image.label :image, class: "name__image__set__file" do = image_tag("icon_camera.png", class: 'camera', height: '20', width: '18') = item_image.file_field :image, class: "hidden" %p ドラッグアンドドロップ %br またはクリックしてファイルをアップロード .image__message 画像がありません .item-explain %label.label-edit 商品名 %sapn.required 必須 .item-explain__text = f.text_field :item_name, class: "item-explain__text__form", placeholder: "40文字まで" %p 入力してください %label.label-edit 商品の説明 %sapn.required 必須 .item-explain__text__textarea = f.text_area :content, placeholder: "商品の説明(必須 1,000文字以内)\n(色、素材、重さ、定価、注意点など)\n\n例)2010年頃に1万円で購入したジャケットです。ライトグレーで傷はありませ\nん。あわせやすいのでおすすめです。" -# %textarea{placeholder: "商品の説明(必須 1,000文字以内)\n(色、素材、重さ、定価、注意点など)\n\n例)2010年頃に1万円で購入したジャケットです。ライトグレーで傷はありませ\nん。あわせやすいのでおすすめです。"} %p 入力してください .item-show %p 商品の詳細 .item-show__list %label.label-edit カテゴリー %sapn.required 必須 = f.select :category_id, Category.all.map { |category| [category.title, category.id] }, { selected: 14 } -# %select -# %option 選択してください -# %option レディース -# %option メンズ -# %option ベビー・キッズ -# %option インテリア・住まい・小物 -# %option 本・音楽・ゲーム -# %option おもちゃ・ホビー・グッズ -# %option コスメ・香水・美容 -# %option 家電・スマホ・カメラ -# %option スポーツ・レジャー -# %option ハンドメイド -# %option チケット -# %option 自動車・オートバイ -# %option その他 .choice 選択してください .item-show__list %label.label-edit ブランド %sapn.item-show__list__any 任意 = f.fields_for @brand do |brand| = brand.text_field :brand, placeholder: "例) シャネル" -# %input{placeholder: "例) シャネル"} .choice 選択してください .item-show__list %label.label-edit 商品の状態 %sapn.required 必須 = f.select :status, { "選択してください": 1, "新品・未使用": 2, "未使用に近い": 3, "目立った傷や汚れなし": 4, "やや傷や汚れあり": 5, "傷や汚れあり": 6, "全体的に状態が悪い": 7 } -# %select -# %option 選択してください -# %option 新品・未使用 -# %option 未使用に近い -# %option 目立った傷や汚れなし -# %option やや傷や汚れあり -# %option 傷や汚れあり -# %option 全体的に状態が悪い .choice 選択してください .delivery %p 配送について .delivery__list %label.label-edit 配送料の負担 %sapn.required 必須 = f.fields_for @shipping do |shipping| = shipping.select :ship_base, { "選択してください": 1, "送料込み(出品者負担)": 2, "着払い(購入者負担)": 3 } .choice 選択してください .delivery__list %label.label-edit 発送までの日数 %sapn.required 必須 = shipping.select :ship_date, { "選択してください": 1, "1日〜2日で発送": 2, "2日〜3日で発送": 3, "4日〜7日で発送": 4 } -# %select -# %option 選択してください -# %option 1日〜2日で発送 -# %option 2日〜3日で発送 -# %option 4日〜7日で発送 .choice 選択してください .item-price %p 価格(¥300〜9,999,999) .item-price__content .item-price__status %label.label-edit 商品の状態 %sapn.required 必須 .item-price__bye %span ¥ = f.text_field :price, placeholder: "0" -# %input{type: 'number', placeholder: '0', class: 'product-price'} %p.form-error 300以上9999999以下で入力してください .fee .fee__price 販売手数料 (10%) .fee__content %span ー .profit .profit__price 販売利益 .profit__content %span ー .exhibilion .exhibilion__button = f.submit "出品する", class: 'exhibilionbutton' -# %button{type: "submit", value: '出品する', class: 'exhibilionbutton'} 出品する %button{type: "submit", class: 'exhibilion__save'} 下書きに保存 %a.top__return もどる %p.warning 禁止されている %a 行為 および %a 出品物 を必ずご確認ください。偽ブランド品や盗品物などの販売は犯罪であり、法律に より処罰される可能性があります。 また、出品をもちまして加盟店規約に同意したことになります。 .product__footer .product__footer__link = link_to 'プライバシーポリシー', '/', class: 'footer-link' = link_to 'フリマ利用規約', '/', class: 'footer-link' = link_to '特定商取引に関する表記', '/', class: 'footer-link' .product__footer__logo = image_tag('logo-white', height: '49', width: '185') %p.logo__tag © FURIMA, Inc.

試したこと

paramsを見てみると、item_imageの中に情報が全て含まれてしまってるので、これはどうにか別々に送信できないかを考えました。
・fields_forごとにform_withを定義し直す
・fields_forをfieldsで試す
・全てのフォームで独自にform_withを定義する

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問