プログラミング初心者です。
分からに事が多くわかりづらい部分があると思いますが、
その点はご指摘いただけますと幸いです。
家計簿アプリを作成しています。
帳簿は複数あり、それぞれの中に利用科目、
またその中に利用細目を1つのビューから保存したいとおもい、
実装していたのですが、ボタンを押してもrenderが動いて
同じビューが表示されるのでエラーを出して何が原因かを確認したところ、
エラー内容
エラーが発生したため bop subject は保存されませんでした。 Account book translation missing: ja.activerecord.errors.models.bop_subject.attributes.account_book.required
というエラーが表示されました。
いろいろと検索してみましたがよくわかりません。
translation => 翻訳?
attributes => 属性?
required => 必要?
accountbookに対してか記述が誤っているということ??
該当のソースコード
account_book.rb
class AccountBook < ApplicationRecord has_many :bop_subjects, dependent: :destroy end
bop_subject.rb
class BopSubject < ApplicationRecord has_many :bop_details, dependent: :destroy belongs_to :account_book enum bop: { balance: 0, payments: 1 } def save_detail(sent_details) self.bop_details << sent_details current_details = self.details.pluck(:name) unless self.details.nil? old_details = current_details - sent_details new_details = sent_details - current_details old_details.each do |old| self.details.delete Detail.find_by(name: old) end new_details.each do |new| new_post_detail = Detail.find_or_create_by(name: new) self.details << new_post_detail end end end
bop_detail.rb
class BopDetail < ApplicationRecord belongs_to :bop_subject end
bop_subject_controller.rb
class Public::BopSubjectsController < ApplicationController def new @bop_subject = BopSubject.new end def create @bop_subject = BopSubject.new(bop_subject_params) @bop_subject.end_user_id = current_end_user.id detail_list = params[:bop_subject][:detail_name] if @bop_subject.save @bop_subject.save_detail(detail_list) redirect_to bop_subject_path(@bop_subject) else render :new end end def show end def edit end private def bop_subject_params params.require(:bop_subject).permit(:subject_name, :bop, :price, :point, :memo, :registered_person_id) end end
bop_subjects/new.html.erb
<div class="col-lg-10 col-md-12 mt-5 mx-auto"> <div class="card"> <div class="m-5"> <h3>収支登録</h3> <div style="display:inline-flex"> </div> <%= form_with model: @bop_subject, url: bop_subjects_path, method: :post do |f| %> <%= render "layouts/error", obj: @bop_subject %> <%= render "public/shared/error_messages", resource: @bop_subject %> <div class="form-group row"> <div class="btn-group col-12" data-toggle="buttons"> <label class="col-6 btn btn-outline-secondary active"> <%= f.radio_button :bop, "payments", {checked: true} %>支出 </label> <label class="col-6 btn btn-outline-secondary"> <%= f.radio_button :bop, "balance" %>収入 </label> </div> </div> <div class="form-group"> <%= f.label :account_book_id, "帳簿" %> <%= f.collection_select :account_book_id, AccountBook.all, :id, :name %> </div> <div class="form-group"> <%= f.label :use_at, "利用日" %> <%= f.date_field :use_at, class: "form-control" %> </div> <div class="form-group"> <%= f.label :price, "利用額" %> <%= f.text_field :price, data: { autonumeric: { aSign: '¥ ', mDec: 0 } }, class: "detail_total_price form-control" %> </div> <div class="form-group"> <%= f.label :subject_name, "利用科目" %> <%= f.text_field :subject_name, class: "form-control" %> </div> <div class="form-group"> <p>利用細目</p> <div style="display:inline-flex"> <%= f.text_field :subject_name, :placeholder => "科目名(例:食費など)", class: "form-control mr-3" %> <%= f.text_field :detail_name, :placeholder => "細目名(例:バナナなど)", class: "form-control mr-3" %> <%= f.text_field :amount, :placeholder => "×3", class: "form-control mr-3" %> <%#= f.text_field : %> <%= f.text_field :detail_price, :placeholder => "¥10,000,000", class: "detail_price form-control" %> </div> </div> <div class="form-group"> <div style="display:inline-flex"> <%= f.label :point, "ポイント利用", class: "col-4"%> <%= f.text_field :point, class: "point form-control" %> <p class="d-flex align-items-center mx-3">➡</p> <div clasS="col-4 align-items-center"> <!--<p clas="total_price ">total_price</p>--> <%= f.text_field :total_price, class: "total_price form-control" %> </div> </div> </div> <div class="form-group"> <%= f.label :store, "利用店" %> <%= f.text_field :store, class: "form-control" %> </div> <div class="form-group"> <%= f.label :memo, "メモ" %> <%= f.text_field :memo, class: "form-control" %> </div> <%= f.submit "登録", class: "btn btn-success col-12" %> <% end %> </div> </div> </div>
試したこと
・アソシエーションの確認
・devise.ja.ymlの作成
・新規作成ページにaccountbookのセレクトボックスを追加
を行ってみたのですが、エラーの解消ができませんでした。
補足情報(FW/ツールのバージョンなど)
rails 6
ruby 3
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/08/12 08:29
2022/08/12 09:47
2022/08/12 14:29
2022/08/12 22:10
2022/08/13 03:45