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

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

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

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

Q&A

解決済

1回答

1072閲覧

Rails 関連付けしたモデルがDBに保存されない

mrn_006

総合スコア1

Ruby on Rails

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

0グッド

0クリップ

投稿2020/11/09 07:23

前提・実現したいこと

プログラミング初学者です。
Rails(5.2.4.4)でレシピ共有サイトを制作しているのですが、
fileds_forを使った投稿機能の実装にてこずっています。

フォーム入力、送信の実行までは動作するのですが、
親モデル(Recipe)に関連付けした子モデル(RecipeIngredient,RecipeStep)がDBに保存されません。

どなたかお力添えいただけると幸いです。

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

送信後、ターミナルで以下のようなメッセージが出ます。
その後、親モデルのデータのみ保存しているようです。
recipe_paramsの書き方が問題でしょうか。

Unpermitted parameters: :recipe_ingredient, :recipe_step

該当のソースコード

recipes_controller.rb

Ruby

1 def new 2 @recipe = Recipe.new 3 @recipe.recipe_ingredients.build 4 @recipe.recipe_steps.build 5 end 6 7 def create 8 recipe = Recipe.new(recipe_params) 9 recipe.user_id = current_user.id 10 recipe.save 11 redirect_to recipe_path(recipe) 12 end 13 14 private 15 def recipe_params 16 params.require(:recipe).permit( 17 :title, :body, :recipe_image, 18 recipe_ingredients_attributes: [:id, :ingredient, :quantity, :_destroy], 19 recipe_steps_attributes: [:id, :step, :step_image, :_destroy] 20 ) 21 end

recipes/new.erb

Ruby

1 <h5>レシピ名</h5> 2 <%= f.text_field :title, size: 60, placeholder: "20文字以内で入力してください" %> 3 <h5>ひとこと</h5> 4 <%= f.text_area :body, size: "60x2" %> 5 <h5>材料</h5> 6 <%= f.fields_for :recipe_ingredient, @recipe.recipe_ingredients.build do |ingredient| %> 7 <%= ingredient.text_field :ingredient %> 8 <%= ingredient.text_field :quantity %> 9 <% end %> 10 <h5>作り方</h5> 11 <%= f.fields_for :recipe_step, @recipe.recipe_steps.build do |step| %> 12 <%= step.attachment_field :step_image %> 13 <%= step.text_area :step %> 14 <% end %> 15 16 <%= f.submit 'レシピを公開する' %>

models/recipe.rb

Ruby

1class Recipe < ApplicationRecord 2 3 belongs_to :user 4 5 has_many :recipe_ingredients, dependent: :destroy 6 accepts_nested_attributes_for :recipe_ingredients 7 8 has_many :recipe_steps, dependent: :destroy 9 accepts_nested_attributes_for :recipe_steps 10 11 attachment :recipe_image 12 13 validates :title, presence: true 14 validates :body, presence: true 15 validates :recipe_image, presence: true 16 17end

質問内容に不備、わかりづらい箇所があればご指摘いただければと思います。
よろしくお願い致します。

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

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

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

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

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

guest

回答1

0

ベストアンサー

ご推察の通り、recipe_paramsの書き方が問題だと思います

ruby

1 params.require(:recipe).permit( 2 :title, :body, :recipe_image, 3 recipe_ingredient: [:ingredient, :quantity], 4 recipe_step: [:step, :step_image] 5 )

としてみてください

投稿2020/11/09 09:05

hukurouo

総合スコア108

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

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

mrn_006

2020/11/09 10:22 編集

ご回答ありがとうございます。やはりそうですよね。 ご教示いただいたコードに書き換えて投稿したところ ``` ActiveModel::UnknownAttributeError in RecipesController#create unknown attribute 'recipe_ingredient' for Recipe. ``` というエラーが発生しました。 fields_forについて、こちら↓のサイトを参考にしておりまして、 https://qiita.com/nakasato_minami/items/5015319292c9f8a93f34 子モデルの値を受け取るために、「fields_forの引数_attributes」という書き方をしておりました。 この辺りのルールを理解しきれていないのですが、 ネストされたパラメータの書き方はご教示いただいたものが基本なのでしょうか。
hukurouo

2020/11/09 11:03

unknown attribute 'recipe_ingredient' for Recipe. Recipeとrecipe_ingredient は紐づいてませんよ、っていうメッセージですね。 コードをよく見てみたら model では accepts_nested_attributes_for :recipe_ingredients と記述されおり :recipe_ingredients とは紐づいていますが、フォームでは :recipe_ingredient となっていますね。ここを複数形にしてみてください。↓のとこです。:recipe_step も同様です。 <%= f.fields_for :recipe_ingredient, @recipe.recipe_ingredients.build do |ingredient| %> <%= ingredient.text_field :ingredient %> <%= ingredient.text_field :quantity %> <% end %> あとはストロングパラメータのとこも複数形に直す必要がありますね。 ネストするStrong Parametersの書きかたについては、以下をご参照ください。 https://qiita.com/kymmt90/items/4ce8618ca8f537b2ef7e
mrn_006

2020/11/09 13:02

フォームとストロングパラメータを複数形にしたら保存に成功しました! 「1対多」の関係から、複数形で書くべきということですかね。 詳しく丁寧に教えてくださりありがとうございます。大変助かりました。 URLもありがとうございます。この辺り、しっかり学んでいこうと思います。
hukurouo

2020/11/09 13:08

>「1対多」の関係から、複数形で書くべきということですかね。 複数形で書くほうが自然なので、望ましいという感じですね。 accepts_nested_attributes_for :recipe_ingredient とモデルで定義してあげれば単数形で書いても動きはします。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問