super-appy / itsumono_obento

今ある材料で作れるレシピを提案するAIレシピとお弁当カレンダー機能で、お弁当作りをサポートするアプリ
6 stars 0 forks source link

バリデーションとエラーメッセージ #81

Closed super-appy closed 8 months ago

super-appy commented 8 months ago

logのコメントの上限を100文字にしようと思う

super-appy commented 8 months ago

Reipesコントローラで更新時に以下のエラーがでていた。

Unpermitted parameter: :from_new_api. Context: { controller: RecipesController, action: create, request: #<ActionDispatch::Request:0x0000ffffb79f3988>, params: {"authenticity_token"=>"[FILTERED]", "recipe"=>{"title"=>"", "recipe_ingredients_attributes"=>{"0"=>{"name"=>"", "quantity"=>"", "_destroy"=>"false"}, "1"=>{"name"=>"", "quantity"=>"", "_destroy"=>"false"}, "2"=>{"name"=>"", "quantity"=>"", "_destroy"=>"false"}}, "recipe_steps_attributes"=>{"0"=>{"number"=>"1", "description"=>"", "_destroy"=>"false"}, "1"=>{"number"=>"2", "description"=>"", "_destroy"=>"false"}, "2"=>{"number"=>"3", "description"=>"", "_destroy"=>"false"}}, "time_required"=>"", "taste"=>"", "tag_ids"=>["", "", ""], "from_new_api"=>"false"}, "button"=>"", "controller"=>"recipes", "action"=>"create"} }

ストロングパラメーターに含まれていないのが原因のようなので、おとなしく追加してみる。 すると、また別のエラーが出た。

ActiveModel::UnknownAttributeError at /recipes
unknown attribute 'from_new_api' for Recipe.

レシピモデルに、from_new_apiはないよとのこと...

super-appy commented 8 months ago

ストロングパラメーターに属性を追加して、Recipe.new(recipe_params)の前で、その属性を削除することでエラー回避!

super-appy commented 8 months ago

そもそもバリデーションメッセージが表示されなかった原因 https://qiita.com/soraa24926/items/b169d9807124e69158ad

super-appy commented 8 months ago

Recipeのnewアクションでバリデーションが表示されるようになったけど 読み込み直したらフォームの数が3倍に増えてしまっていた!

ビューで3.timesでフォームを作っていたのが原因。コントローラに移管して、newの時だけ作られるように変更。

super-appy commented 8 months ago

recipeのバリデーションは一部、現状のままではかけられないことが判明。 大工事になるけど、コントローラを分けることに決定!!!!