Q&A
前提
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にしたりしましたがエラー画面は変わりませんでした。
分かる方がいましたらご教授ください。
よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
2022/05/27 02:58
2022/06/10 01:50