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

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

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

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

Ruby on Rails 6

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

MVC

MVC(Model View Controller)は、オブジェクト指向プログラミングにおけるモデル・ビュー・コントローラーの総称であり、ソフトフェア開発で使われている構築パターンとしても呼ばれます。

Ruby on Rails

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

オブジェクト

オブジェクト指向において、データとメソッドの集合をオブジェクト(Object)と呼びます。

Q&A

解決済

1回答

1179閲覧

Updateができません。助けてください!

matsudak

総合スコア1

Ruby

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

Ruby on Rails 6

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

MVC

MVC(Model View Controller)は、オブジェクト指向プログラミングにおけるモデル・ビュー・コントローラーの総称であり、ソフトフェア開発で使われている構築パターンとしても呼ばれます。

Ruby on Rails

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

オブジェクト

オブジェクト指向において、データとメソッドの集合をオブジェクト(Object)と呼びます。

0グッド

0クリップ

投稿2021/05/21 04:40



実装したいこと

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

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

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

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

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

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

guest

回答1

0

ベストアンサー

recipe = Recipe.update(image: image, title: title, description: description, people: people, level_id: level_id, user_id: user_id)

これですと、すべてのRecipeレコードが更新され更新が行われた要素が配列で返ってきます。
updateのリファレンスをご確認ください

投稿2021/05/21 05:35

asm

総合スコア15147

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

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

matsudak

2021/05/21 06:29

早速の回答ありがとうございます。 URL見さしていただきましたが、よくわかりませんでした。。。 # Updates one record Person.update(15, user_name: "Samuel", group: "expert") ここの部分を実装に移し替えるどういう風に表記したらいいのかがいまいちピンときてないです。。。
asm

2021/05/21 08:06

Recipe.update Ingredient.update Procedure.update それぞれ、どのレコード(具体的にはidとして何が格納されているのか)を更新したいのかを明確にしてください 例えば、Recipeの場合はparams[:id]になるかとおもいますが RecipeIngredientProcedureに渡していないので渡す必要がありますね
matsudak

2021/05/21 09:07

返信ありがとうございます。 コントローラーでは、find(params[:id])でレコードを取得できると思うのですが、 モデルの場合のコードがわからないです。。。
asm

2021/05/21 11:06

コントローラから @recipe_ingredient_procedure.update(@recipe) 的に渡してやり RecipeIngredientProcedure#update側は def update(recipe) recipe.update(....) みたいな感じではいかがですか? Ingredientおよび、Procedureの方も同様にidもしくはレコードを取得する必要がありますが
matsudak

2021/05/25 03:51

返信いただいていたのに、回答遅れました。すみません。。。 なんとか実装できました!!ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問