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

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

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

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

Ruby on Rails 6

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

Q&A

解決済

1回答

1177閲覧

Mysql2::Error: Field 'information_id' doesn't have a default valueが解決できなくて困っています。

milktea246

総合スコア23

Ruby

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

Ruby on Rails 6

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

0グッド

0クリップ

投稿2022/05/26 14:49

前提

rails初学者です。
ペットの登録してある情報に紐づいたペットのその日の健康状態を記録していく日記型のアプリを作成中です。
views/details/new.html.erbにペットの情報(information)を参照した上でとペットの健康状態(detail)を入力できる画面を作り、投稿ボタンを押すことでペットの健康状態のみをDBに登録できるようにしたいです。

実現したいこと

Mysql2::Error: Field 'information_id' doesn't have a default value
のエラーを解決し、ペット情報に紐づいたペットの健康状態を登録できるようにしたい。

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

https://gyazo.com/539f2a09460c6d6dd85fe0b35ec0d936

該当のソースコード

details_controller.rb

class DetailsController < ApplicationController def index end def new @information = Information.find(params[:id]) @detail = Detail.new end def create @information = Information.find(params[:information_id]) @detail = Detail.new(detail_params) if @detail.save redirect_to information_detail_path(@information, @detail) else render :new end end def show end def edit end private def detail_params params.require(:detail).permit(:information, :rice, :medicine, :vaccination, :vaccination_image, :weight, :motion, :snack, :remarks) end end

new.html.erb

<section class="hero is-warning"> <div class="hero-body"> <div class="container"> <h1 class="title"> ペットの記録 </h1> </div> </div> </section> <section class="section"> <div class="container"> <div class="columns is-centered"> <div class="column is-4"> <div class="card"> <div class="card-image"> <figure class="image is-4by5"> <img class="is-rounded"><%= attachment_image_tag @information, :pet_image %> </figure> </div> <div class="card-content"> <div class="media"> <div class="media-content"> </div> </div> <div class="content"> <table class = "table is-striped"> <tbody> <tr> <th>ペット情報</th> </tr> <tr> <td><u>名前:<%= @information.name %></u></td> </tr> <tr> <td><u>性別:<%= @information.sex %></u></td> </tr> <tr> <td><u>種類:<%= @information.kinds %></u></td> </tr> <tr> <td><u>色:<%= @information.color %></u></td> </tr> <tr> <td><u>誕生日:<%= @information.birthday %></u></td> </tr> </body> </table> </div> </div> </div> </div> <div class="column is-6"> <%= form_for @detail, url: information_details_path, method: :post, local: true do |f| %> <%= f.label :rice, "ご飯", class: "label" %> <%= f.text_field :rice, class: "input is-success" %> <%= f.label :medicine, "処方薬", class: "label" %> <%= f.text_field :medicine, class: "input is-success" %> <%= f.label :vaccination, "ワクチン接種の有無", class: "label" %> <%= f.text_field :vaccination, class: "input is-success" %> <%= f.label :vaccination_image, "ワクチン接種証明", class: "label" %> <%= f.attachment_field :vaccination_image, class: "input is-success" %> <%= f.label :weight, "体重", class: "label" %> <%= f.text_field :weight, class: "input is-success" %> <%= f.label :motion, "運動", class: "label" %> <%= f.text_field :motion, class: "input is-success" %> <%= f.label :snack, "お菓子", class: "label" %> <%= f.text_field :snack, class: "input is-success" %> <%= f.label :remarks, "備考", class: "label" %> <%= f.text_field :remarks, class: "input is-success" %> <%= f.submit "投稿", class: "button is-success" %> <br> <br> <% end %> </div> </div> </div> </section>

マイグレーションファイル

class CreateDetails < ActiveRecord::Migration[6.0] def change create_table :details do |t| t.string :rice, null: false t.text :medicine t.string :vaccination t.string :vaccination_image_id t.integer :weight, null: false t.string :motion t.string :snack t.text :remarks t.references :information, null: false, foreign_key: true t.timestamps end end end

detail.rb

class Detail < ApplicationRecord belongs_to :information, optional: true attachment :vaccination_image with_options presence: true do validates :rice validates :weight end end

試したこと

エラー画面でinformation_idにデフォルトの値が入っていませんと書いてあるので、detail_paramsでpermitしている値が間違っているのかと思い、information_idにしたりしましたがエラー画面は変わりませんでした。

分かる方がいましたらご教授ください。
よろしくお願いします。

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

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

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

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

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

guest

回答1

0

ベストアンサー

原因paramsの中にinformationというkeyが存在しないので@detailのinformationがなくてエラーになっている

解決方法1
hidden_fieldでinformation_idを渡す

# new.html.erb # form_forの中にこれを入れる <%= f.hidden_field : information_id, :value => @ information.id %>

ruby

1# details_controller.rb 2 private 3 def detail_params 4 params.require(:detail).permit(:information_id, :rice, :medicine, :vaccination, :vaccination_image, :weight, :motion, :snack, :remarks) 5 end

解決方法2
一番しっくりくるかな?

ruby

1# details_controller.rb 2 def create 3 @information = Information.find(params[:information_id]) 4 @detail = @information.details.build(detail_params) 5 if @detail.save 6 redirect_to information_detail_path(@information, @detail) 7 else 8 render :new 9 end 10 end 11 12# :informationを消す 13 private 14 def detail_params 15 params.require(:detail).permit(:rice, :medicine, :vaccination, :vaccination_image, :weight, :motion, :snack, :remarks) 16 end

解決方法3
少し無理矢理な感じがしますね😅

ruby

1# details_controller.rb 2 def create 3 @information = Information.find(params[:information_id]) 4 @detail = @information.details.build(detail_params.merge({ information: @information }) 5 if @detail.save 6 redirect_to information_detail_path(@information, @detail) 7 else 8 render :new 9 end 10 end 11 12# :informationを消す 13 private 14 def detail_params 15 params.require(:detail).permit(:rice, :medicine, :vaccination, :vaccination_image, :weight, :motion, :snack, :remarks) 16 end

投稿2022/05/26 15:28

ohoh5454

総合スコア92

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

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

milktea246

2022/05/27 02:58

ohoh5454様、ご教授ありがとうございます。 教えていただいた3つの方法を試しましたが、解決方法1は同じエラー画面で止まってしまい、解決方法2、3は https://gyazo.com/1728b2224226d4679a59b836386a3887 画像のようにdetailsのメソッドは定義されていませんと表示されてしまいます。 この後はどのような記述に変えれば解決できるでしょうか?
milktea246

2022/06/10 01:50

information.rbにてdetail.rbに対しての一対多の関係のアソシエーションが組まれていないことで上記のようなエラーが出てしまっていました。アソシエーションを記述することでエラーを解決でき、健康状態のみをDBに登録することが出来ました。最終的に解決方法2を使って解決することが出来ました。 ohoh5454様、ありがとうございました!!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問