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

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

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

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

Q&A

1回答

1047閲覧

Rails fields_forがうまくいきません

yu-ka5454

総合スコア3

Ruby on Rails

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

0グッド

0クリップ

投稿2021/03/31 10:02

現在複数のモデルの情報を一括で保存するフォームを作成しています。

以下のようなエラーが出て丸一日解決せず、皆様の知見をお借りしたいです。

エラー文

ActiveModel::UnknownAttributeError at /family/tops unknown attribute 'vitals_attributes' for Vital.

なぜかストロングパラメータで_attribeの部分がカラムとして認識されているようで、引っかかってしまいます。。

モデル

class Family < ApplicationRecord has_secure_password has_many :vitals accepts_nested_attributes_for :vitals has_many :bathing_days accepts_nested_attributes_for :bathing_days has_many :intake_waters accepts_nested_attributes_for :intake_waters has_many :medicines accepts_nested_attributes_for :medicines has_many :excretas accepts_nested_attributes_for :excretas has_many :meals accepts_nested_attributes_for :meals has_many :behavior_histories accepts_nested_attributes_for :behavior_histories has_many :entries, dependent: :destroy has_many :events, through: :entries

フォーム

<div class="form-container"> <h1>一括登録フォーム</h1> <%= form_with model: @family, url: :family_tops, local: true do |form| %> <p>バイタル</p> <%= form.fields_for :vitals, @family.vitals.build do |a| %> <div> <%= a.label :measuring_date, "日付" %> <%= a.date_field :measuring_date, required: true %> </div> <div> <%= a.label :temperature, "体温" %> <%= a.number_field :temperature, required: true %> </div> <div> <%= a.label :hign_blood_pressure, "血圧(上)" %> <%= a.number_field :hign_blood_pressure, placeholder: "血圧を入力", required: true %> </div> <div> <%= a.label :low_blood_pressure, "血圧(下)" %> <%= a.number_field :low_blood_pressure, placeholder: "血圧を入力", required: true %> </div> <div> <%= a.label :pulse, "脈拍" %> <%= a.number_field :pulse, placeholder: "脈拍を入力", required: true %>回 </div> <div> <%= a.label :breathing, "呼吸回数/1分" %> <%= a.number_field :breathing, placeholder: "呼吸回数を入力", required: true %> </div> <div> <%= a.label :spo2, "spo2" %> <%= a.number_field :spo2, placeholder: "spo2を入力", required: true %>%% </div> <% end %> --------------------------- <p>入浴日</p> <%= form.fields_for :bathing_days, @family.bathing_days.build do |b| %> <div> <%= b.label :bathing_date, "入浴日" %> <%= b.date_field :bathing_date, required: true %> </div> <div> <%= b.label :judge_bath, "入浴したらチェック" %> <%= b.check_box :judge_bath, required: true %> </div> <% end %> --------------------------- <p>水分量</p> <%= form.fields_for :intake_waters, @family.intake_waters.build do |c| %> <div> <%= c.label :drink_date, "日付" %> <%= c.date_field :drink_date, required: true %> </div> <div> <%= c.label :amount_of_water, "水分量" %> <%= c.number_field :amount_of_water, required: true %>ml </div> <% end %> --------------------------- <p>服薬チェック</p> <%= form.fields_for :medicines, @family.medicines.build do |d| %> <div> <%= d.label :medicine_date, "日付" %> <%= d.date_field :medicine_date, required: true %> </div> <div> <%= d.label :check_before_breakfast, "朝食前" %> <%= d.check_box :check_before_breakfast, required: true %> </div> <div> <%= d.label :check_after_breakfast, "朝食後" %> <%= d.check_box :check_after_breakfast, required: true %> </div> <div> <%= d.label :check_after_lunch, "昼食前" %> <%= d.check_box :check_after_lunch, required: true %> </div> <div> <%= d.label :check_after_lunch, "昼食後" %> <%= d.check_box :check_after_lunch, required: true %> </div> <div> <%= d.label :check_before_breakfast, "夕食前" %> <%= d.check_box :check_before_breakfast, required: true %> </div> <div> <%= d.label :check_after_dinner, "夕食後" %> <%= d.check_box :check_after_dinner, required: true %> </div> <div> <%= d.label :check_eye_drops, "目薬" %> <%= d.check_box :check_eye_drops, required: true %> </div> <div> <%= d.label :check_ointment, "軟膏" %> <%= d.check_box :check_ointment, required: true %> </div> <% end %> --------------------------- <p>排泄記録</p> <%= form.fields_for :excretas, @family.excretas.build do |e| %> <div> <%= e.label :excreta_date, "日付" %> <%= e.date_field :excreta_date, required: true %> </div> <div> <%= e.label :judge_pee, "排尿" %> <%= e.check_box :judge_pee, required: true %> </div> <div> <%= e.label :judge_shit, "排便" %> <%= e.check_box :judge_shit, required: true %> </div> <% end %> --------------------------- <p>食事量</p> <%= form.fields_for :meals, @family.meals.build do |e| %> <div> <%= e.label :breakfast, "朝食" %> <%= e.check_box :breakfast, required: true %> </div> <div> <%= e.label :lunch, "昼食" %> <%= e.check_box :lunch, required: true %> </div> <div> <%= e.label :snack, "おやつ" %> <%= e.check_box :snack, required: true %> </div> <div> <%= e.label :dinner, "夕食" %> <%= e.check_box :dinner, required: true %> </div> <% end %> --------------------------- <p>活動記録</p> <%= form.fields_for :behavior_histories, @family.behavior_histories.build do |e| %> <div> <%= e.label :behavior_history_date, "日付" %> <%= e.date_field :behavior_history_date, required: true %> </div> <div> <%= e.label :behavior_time, "時間" %> <%= e.time_field :behavior_time, required: true %> </div> <div> <%= e.label :action_record, "活動記録" %> <%= e.text_area :action_record, required: true %> </div> <% end %> <div> <%= form.submit "登録する" %> </div> <% end %> </div>

コントローラー

class Family::TopsController < Family::Base #一括フォーム用のコントローラー def main @family = Family.find(1) end def index end def show @care_recipitent = CareRecipitent.find(params[:id]) end def new @family = Family.new @vital = @family.vitals.new @bathing_day = @family.bathing_days.new @intake_water = @family.intake_waters.new @medicine = @family.medicines.new @excreta = @family.excretas.new @meal = @family.meals.new @behavior_history = @family.behavior_histories.new end def create if @family = current_family.vitals.create!(family_params) @family = current_family.bathing_days.create!(family_params) @family = current_family.intake_waters.create!(family_params) @family = current_family.medicines.create!(family_params) @family = current_family.excretas.create!(family_params) @family = current_family.meals.create!(family_params) @family = current_family.behavior_histories.create!(family_params) redirect_to family_family_tops_path else render :new end end private def family_params params[:family].permit(vitals_attributes:[:measuring_date, :temperature, :hign_blood_pressure, :low_blood_pressure, :breathing, :spo2, :pulse], bathing_days_attributes:[:bathing_date, :judge_bath], intake_waters_attributes:[:drink_date, :amount_of_water], medicines_attributes:[:medicine_date, :check_before_breakfast, :check_after_breakfast, :check_before_lunch, :check_after_lunch, :check_before_dinner, :check_after_dinner, :check_ointment, :check_eye_drops], excretas_attributes:[:excreta_date, :judge_shit, :judge_pee], meals_attributes:[:meal_date, :breakfast, :lunch, :snack, :dinner], behavior_histories_attributes:[:behavior_history_date, :behavior_time, :action_record]) end end

パラメータ

{"authenticity_token"=>"kOXV4yiPlJFNU3+f/FUjRBvcdlpq4bWgl4aZCP6gYQRdkwXI/iBpZv63erklPc03RwW33unyJuG2xY3sTYm7Ww==", "family"=>{"vitals_attributes"=>{"0"=>{"measuring_date"=>"2021-03-04", "temperature"=>"445", "hign_blood_pressure"=>"435", "low_blood_pressure"=>"35", "pulse"=>"345", "breathing"=>"345", "spo2"=>"345"}}, "bathing_days_attributes"=>{"0"=>{"bathing_date"=>"2021-03-10", "judge_bath"=>"1"}}, "intake_waters_attributes"=>{"0"=>{"drink_date"=>"2021-03-25", "amount_of_water"=>"435"}}, "medicines_attributes"=>{"0"=>{"medicine_date"=>"2021-03-18", "check_before_breakfast"=>"1", "check_after_breakfast"=>"1", "check_after_lunch"=>"1", "check_after_dinner"=>"1", "check_eye_drops"=>"1", "check_ointment"=>"1"}}, "excretas_attributes"=>{"0"=>{"excreta_date"=>"2021-03-24", "judge_pee"=>"1", "judge_shit"=>"1"}}, "meals_attributes"=>{"0"=>{"breakfast"=>"1", "lunch"=>"1", "snack"=>"1", "dinner"=>"1"}}, "behavior_histories_attributes"=>{"0"=>{"behavior_history_date"=>"2021-03-23", "behavior_time"=>"19:51", "action_record"=>"fsg"}}}, "commit"=>"登録する", "host"=>"kiroku2.application.com", "controller"=>"family/tops", "action"=>"create"}

よろしくお願いいたしますm(._.)m

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

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

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

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

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

guest

回答1

0

current_family.vitals.create!(family_params)
ここでは Family をつくらずに Vital を作っています。
Viatlには accepts_nested_attributes_for :vitals していないです
current_family.vitals.create!(family_params[:vitals_attributes])

投稿2021/03/31 13:34

winterboum

総合スコア23401

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

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

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

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問