実装したいこと
formオブジェクトのupdateができません。
paramsで編集したい値は取れています。
#エラー内容
NoMethodError in RecipesController#update
undefined method `id' for #Array:0x00007fcd7e97f6c8
#ソースコード
recipes_controller.rb
class RecipesController < ApplicationController before_action :authenticate_user!, except: [:index, :show] 〜略〜 def edit @recipe = Recipe.find(params[:id]) @recipe_ingredient_procedure = RecipeIngredientProcedure.new end def update @recipe = Recipe.find(params[:id]) @recipe_ingredient_procedure = RecipeIngredientProcedure.new(recipe_params) if @recipe_ingredient_procedure.valid? @recipe_ingredient_procedure.update redirect_to root_path else render :edit end end 〜略〜 private def recipe_params params.require(:recipe_ingredient_procedure).permit(:image, :title, :description, :people, :level_id, :ingredient, :amount, :procedure, ingredients: [:ingredient, :amount], procedures: [:procedure]).merge( user_id: current_user.id ) end end
recipe_ingredient_procedure.rb
class RecipeIngredientProcedure include ActiveModel::Model attr_accessor :image, :title, :description, :people, :level_id, :user_id, :procedure, :procedures, :ingredient, :amount, :ingredients 〜略〜 def save recipe = Recipe.new(image: image, title: title, description: description, people: people, level_id: level_id, user_id: user_id) recipe.save if recipe.valid? ingredients.each do |ingredient| value = ingredient[1] value2 = Ingredient.new(ingredient: value[:ingredient], amount: value[:amount], recipe_id: recipe.id) value2.save if value2.valid? end procedures.each do |procedure| value3 = procedure[1] value4 = Procedure.new(procedure: value3[:procedure], recipe_id: recipe.id) value4.save if value4.valid? end end def update recipe = Recipe.update(image: image, title: title, description: description, people: people, level_id: level_id, user_id: user_id) ingredients.each do |ingredient| value5 = ingredient[1] value6 = Ingredient.update(ingredient: value5[:ingredient], amount: value5[:amount], recipe_id: recipe.id) end procedures.each do |procedure| value7 = procedure[1] value8 = Procedure.update(procedure: value7[:procedure], recipe_id: recipe.id) end end end
edit.html.erb
<%= form_with model: @recipe_ingredient_procedure, url: recipe_path(@recipe.id), method: :patch, id: 'recipe-update', class: 'col-lg-6 text-center mx-auto', local: true do |f| %> <br><br> <div class='form-header'> <h1 class='form-header-text'> レシピ編集 </h1> </div> <br> <%= render 'shared/error_messages', model: f.object %> <div class="row"> <label for="title" class="form-label">料理名</label> <%= f.text_field :title, class:"form-control", id:"title", placeholder:"例) えびとブロッコリーのアヒージョ", value: "#{@recipe.title}" %> </div> <div class="row"> <label for="description" class="form-label">料理の画像</label> <%= f.file_field :image, class:"form-control", id:"cook-image", value: "#{@recipe.image}" %> </div> <div class="row"> <label for="level" class="form-label">料理の難易度</label> <%= f.collection_select(:level_id, Level.all, :id, :name, {}, {class:"form-control", value: "#{@recipe.level.name}"}) %> </div> <div class="row"> <label for="description" class="form-label">料理の説明</label> <%= f.text_area :description, class:"form-control", id:"description", placeholder:"papapapappapapapa", value: "#{@recipe.description}"%> </div> <div class="row"> <label for="people" class="form-label">何人前</label> <%= f.text_field :people, class:"form-control", id:"people", placeholder:"例) 1〜2人前", value: "#{@recipe.people}" %> </div> <div class="d-flex justify-content-around"> <div class="flex-column"> <label for="ingredient" class="form-label">材料名</label> </div> <div class="flex-column"> <label for="amount" class="form-label">分量</label> </div> </div> <% 10.times do |i| %> <%= f.fields_for "ingredients[#{i}]" do |field| %> <div class="d-flex justify-content-around"> <div class="flex-column"> <div class="row"> <%= field.text_field :ingredient, class:"form-control", id:"ingredient", placeholder:"例) 砂糖", value: "#{@recipe.ingredients[i].ingredient}" %> </div> </div> <div class="flex-column"> <div class="row"> <%= field.text_field :amount, class:"form-control", id:"amount", placeholder:"例) 大さじ1", value: "#{@recipe.ingredients[i].amount}" %> </div> </div> </div> <% end %> <% end %> <label for="procedure" class="form-label">料理手順</label> <% 6.times do |i| %> <%= f.fields_for "procedures[#{i}]" do |field| %> <div class="procedures"> <div class="row"> <%= "料理工程#{i + 1}"%> <%= field.text_area :procedure, class:"form-control", id:"procedure", placeholder:"例) ①調味料の〇〇と〇〇を混ぜる", value: "#{@recipe.procedures[i].procedure}" %> </div> </div> <% end %> <% end %> <br> <button type="submit" class="btn btn-primary new-btn">Submit</button> <% end %>
#試したこと
上記コードのrecipes_controller.rbのupdateにbinding.pryを入れると
paramsに値は全て入っています。
recipe_ingredient_procedure.rbのコードが間違っていると思うのですが
三日間手付かずです。。。
助けてくださいorz
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/05/21 06:29
2021/05/21 08:06
2021/05/21 09:07
2021/05/21 11:06
2021/05/25 03:51