LoginSignup
hiddy0329
@hiddy0329 (hiddy)

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

複数テーブルへのデータ保存エラー

解決したいこと

Ruby on Railsで伝票作成アプリを実装中です。
伝票を作成する画面から情報を登録しようとしたときにエラーが発生しました。

発生している問題・エラー

以下のような伝票作成画面から伝票情報を入力します。
伝票作成画面.png

「出荷先」、「出荷日」、「伝票番号」、「送り状ナンバー」の4項目はslipsテーブルへ保存し、
「品番」、「色」、「数量」、「備考」の4項目はordersテーブルへ一括登録するように実装しています。

そのため、以下のコードのように実装しましたが、登録ボタンを押してもデータベースに保存されません。

該当するソースコード

app/controllers/slips_controller.rb

class SlipsController < ApplicationController
  before_action :authenticate_user!

  def new
    @slip = Slip.new
    10.times do
      @order = @slip.orders.new
    end
  end

  def create
    @slip = Slip.new(slip_params)
    if @slip.save
      redirect_to root_path
    else
      render :new
    end
  end

  private

  def slip_params
    params.require(:slip).permit(:address_name, :shipping_date, :slip_number, :invoice_number, orders_attributes: [:order_number, :color, :count, :note, :_destroy, :id])
  end
end

app/models/slip.rb

class Slip < ApplicationRecord
  with_options presence: true do
    validates :address_name
    validates :shipping_date
    validates :slip_number, length: { maximum: 11 }
  end

  belongs_to :user

  has_many :slip_products
  has_many :products, through: :slip_products

  has_many :slip_clients
  has_many :clients, through: :slip_clients

  has_many :orders, dependent: :destroy 
  accepts_nested_attributes_for :orders, allow_destroy: true
end

app/models/order.rb

class Order < ApplicationRecord
  with_options presence: true do
    validates :order_number, length: { maximum: 11 }
    validates :color
    validates :count
  end
  belongs_to :slip
end

app/views/slips/new.html.erb

<div class="d-grid gap-5">
<h2 class="text-center bg-primary text-white">新規伝票作成</h2>

<%= form_with model: @slip, url: slips_path, method: :post do |fb| %>

<div class="container">
  <div class="row">
    <div class="col-md-6">
      <th>【出荷先】</th>
      <%= fb.collection_select :address_name, Client.select(:name).distinct, :name, :name, {include_blank: "--"}, class: "form-control" %>
    </div>
    <div class="col-md-6">
      <th>【送り主】</th>
      <p class="h2">
      <%= current_user.name %>
      </p>
    </div>
  </div>
</div>

<div class="container">
  <div class="row">
    <div class="col-md-4">
      <th>【出荷日】</th>
      <%= fb.date_field :shipping_date, class: 'form-control' %>
    </div>
    <div class="col-md-4">
      <th>【伝票番号】</th>
      <%= fb.text_field :slip_number, class: 'form-control' %>
    </div>
    <div class="col-md-4">
      <th>【送り状ナンバー】</th>
      <%= fb.text_field :invoice_number, class: 'form-control' %>
    </div>
  </div>
</div>

<div class="container">
  <table class="table">
    <thead>
      <tr>
        <th>品番</th>
        <th>色</th>
        <th>数量</th>
        <th>備考</th>
      </tr>
    </thead>
    <tbody class="bulk-registration-form">
      <%= fb.fields_for :orders do |f| %>
      <tr class="item">
        <td>
          <%= f.collection_select :order_number, Product.select(:item_number).distinct, :item_number, :item_number, {include_blank: "--"}, class: "form-control" %>
        </td>
        <td>
          <%= f.collection_select :color, Product.select(:color).distinct, :color, :color, {include_blank: "--"}, class: "form-control" %>
        </td>
        <td>
          <%= f.text_field :count, class: 'form-control' %>
        </td>
        <td>
          <%= f.text_field :note, class: 'form-control' %>
        </td>
      </tr>
      <% end %>
    </tbody>
  </table>
</div>

<div class="text-center">
<%= fb.submit '登録', class: 'btn btn-primary' %>
<%= link_to "戻る", root_path, class: 'btn btn-success' %>
</div>

<% end %>

</div>


ターミナル画面

paramsを確認すると、データは取得できています。
複数テーブルへの保存ターミナル画面.png

自分で試したこと

以下のサイトを参考に実装しました。
https://qiita.com/koki_73/items/bc4ca80ab43e84d9704f

私としてはコントローラーの記述に問題があるのではないかと思うのですが、
是非ともご教示いただきたいです。

0

1Answer

エラーがはっきりわかっているときはそのメッセージを張りましょう。

どのようなエラーかはわかりませんが、ORDERは予約語なのでモデル名に使うと「ActiveRecord::SubclassNotFound」といったエラーが出るかもしれません。

0

Comments

  1. @hiddy0329

    Questioner
    単純なアソシエーションの記述忘れでした。
    ありがとうございました!

Your answer might help someone💌