(2026年10月29日 追記:記事どおりに再現できるよう、必要なコードと手順を追加しました)
見積書は、1枚の紙の中に「宛先・件名・日付」と「明細の行(品名・数量・単価)」が同居しています。Excelなら行をコピーして足すだけですが、システムにするときは「1枚の見積書」と「何行もの明細」をどう持つかを最初に決める必要があります。この第3回では、見積書(親)と明細行(子)をRuby on Rails 8で作り、明細行を画面上でその場で追加・削除できるところまで進めます。
前回の第2回:ログインを付けるで、ログインと3段階の権限(閲覧のみ/担当者/管理者)を入れました。今回の見積書の画面も、その権限の仕組みにそのまま乗せます。
「Excelの見積書は行を足すだけなのに、システムにすると『明細を増やす』だけで追加費用と言われました。そんなに大変なことなんですか?」
先に結論です。見積書と明細を「親と子」として設計し、明細の追加・削除を1つの保存操作にまとめれば、Railsではかなり少ない部品で作れます。 大変になりやすいのは明細行そのものではなく、その先の「金額・消費税・端数の計算」(第4回)と「あとから金額が変わらない仕組み」のほうです。今回は後者の土台として、明細に品名や単価を「写して」持たせる設計も入れます。
この記事では、モデルの設計、入れ子のフォーム(=1つの画面で親と子を一緒に保存する入力欄)、明細行を追加する小さなJavaScript、そして自動テストまでを、実際に動かしたコードで解説します。
見積書と明細を「親と子」で持つ考え方
まず、データの持ち方を整理します。見積書のデータは、次の2つの表に分けて持ちます。
| テーブル | 1行が表すもの | 主な項目 |
|---|---|---|
| estimates(見積書) | 見積書1枚 | 取引先、見積番号、件名、発行日、有効期限、備考 |
| estimate_lines(見積明細) | 見積書の中の1行 | 見積書への紐づけ、並び順、品名、単位、数量、単価、消費税率 |
1枚の見積書に明細が何行もつながる関係なので、見積書が「親」、明細が「子」です。Railsでは、これを has_many(=1対多の関係)と belongs_to で表します(モデルの関連づけの公式ガイドは下記です)。
📰 出典:Rails Guides「Active Record Associations」
ここで大事なのが、明細が品目マスタを「参照」するだけでなく、品名・単価・税率を「写して」持つ点です。
- 品目マスタの単価を後から改定したとき、過去の見積書の金額が勝手に変わってはいけない
- 見積書は「その時点の条件で出した書類」なので、後から変わらないのが当たり前
そのため明細には、品目との紐づけ(item_id)を任意で持たせつつ、品名・単位・単価・税率は明細自身の列として保存します。品目を選んで入力を楽にする機能は、次回以降に足せます。
実装手順
0. 雛形を生成する
見積書は scaffold で、明細はモデルだけを生成します。明細のテーブルは、見積書と同じマイグレーションの中で作るので --no-migration を付けます。
bin/rails generate scaffold Estimate customer:references number:string:uniq title:string \
issued_on:date valid_until:date note:text --no-test-framework --no-helper --no-jbuilder
bin/rails generate model EstimateLine estimate:references item:references position:integer \
name:string unit:string quantity:integer unit_price:integer tax_rate:integer --no-migration --no-test-framework
rm app/views/estimates/_estimate.html.erb
number:string:uniq は、見積番号に重複を許さない索引を付ける指定です。一覧・詳細で使わない部品(_estimate.html.erb)は消します。マイグレーションのファイル名の数字は実行した日時になるので、記事の 20261002184505 と読み替えてください。
1. テーブルを作る(マイグレーション)
生成された db/migrate/20261002184505_create_estimates.rb に、必須の指定と明細のテーブルを足します。次の変更を加えます(+ が追加、- が削除)。
--- a/db/migrate/20261002184505_create_estimates.rb
+++ b/db/migrate/20261002184505_create_estimates.rb
@@ -3,13 +3,26 @@ class CreateEstimates < ActiveRecord::Migration[8.1]
create_table :estimates do |t|
t.references :customer, null: false, foreign_key: true
t.string :number
- t.string :title
- t.date :issued_on
+ t.string :title, null: false
+ t.date :issued_on, null: false
t.date :valid_until
t.text :note
t.timestamps
end
add_index :estimates, :number, unique: true
+
+ create_table :estimate_lines do |t|
+ t.references :estimate, null: false, foreign_key: true
+ t.references :item, foreign_key: true
+ t.integer :position, null: false, default: 0
+ t.string :name, null: false
+ t.string :unit
+ t.integer :quantity, null: false, default: 1
+ t.integer :unit_price, null: false, default: 0
+ t.integer :tax_rate, null: false, default: 10
+
+ t.timestamps
+ end
end
end
foreign_key: true は、存在しない取引先や見積書に紐づくデータを、データベース側で作れなくする指定です。アプリのチェックをすり抜けても、データベースが最後の砦になります。なお、金額は小数の誤差が出ないよう、すべて整数(円)で持ちます。
2. 親と子のモデルを書く
生成された app/models/estimate.rb に次の変更を加えます(+ が追加、- が削除)。
--- a/app/models/estimate.rb
+++ b/app/models/estimate.rb
@@ -1,3 +1,28 @@
class Estimate < ApplicationRecord
belongs_to :customer
+ # 明細は見積書と一緒に保存・削除する。空の行(品名なし)は保存しない
+ has_many :lines, -> { order(:position, :id) }, class_name: "EstimateLine", dependent: :destroy, inverse_of: :estimate
+ accepts_nested_attributes_for :lines, allow_destroy: true, reject_if: ->(attrs) { attrs["name"].blank? }
+
+ validates :title, presence: true, length: { maximum: 100 }
+ validates :issued_on, presence: true
+ validates :valid_until, comparison: { greater_than_or_equal_to: :issued_on }, allow_blank: true, if: :issued_on
+ validate :must_have_line
+
+ after_create :assign_number
+
+ # 税抜の合計(税の計算は第4回)
+ def subtotal
+ lines.reject(&:marked_for_destruction?).sum(&:amount)
+ end
+
+ private
+ # 見積番号は保存後のIDから作る(例: Q-00012)。欠番になりにくく、重複しない
+ def assign_number
+ update_column(:number, format("Q-%05d", id))
+ end
+
+ def must_have_line
+ errors.add(:lines, "を1行以上入力してください") if lines.reject(&:marked_for_destruction?).empty?
+ end
end
ポイントを順に説明します。
accepts_nested_attributes_for :lines:見積書の保存と一緒に、明細の追加・更新・削除もまとめて受け付けるRailsの仕組みです。これがあると、1回の「保存する」ボタンで親と子が一括で保存されます。allow_destroy: true:画面から「この行を削除」と伝えられるようにします(後述の_destroy)。reject_if:品名が空の行は、エラーにせず無視します。画面に空の行が残っていても保存が止まらないための配慮です。dependent: :destroy:見積書を消したら、明細も一緒に消します。must_have_line:明細が1行もない見積書は保存できません。削除予定の行(marked_for_destruction?)は数えないのがコツです。- 見積番号:保存した後にIDから
Q-00012の形で作ります。今回は小さく始めるための簡易的な方式で、年度ごとの連番や欠番を許さない運用が必要な場合は、請求書の採番を扱う第7回で改めて考えます。
明細側の app/models/estimate_line.rb は短く済みます。生成された app/models/estimate_line.rb に次の変更を加えます(+ が追加、- が削除)。
--- a/app/models/estimate_line.rb
+++ b/app/models/estimate_line.rb
@@ -1,4 +1,12 @@
class EstimateLine < ApplicationRecord
belongs_to :estimate
- belongs_to :item
+ belongs_to :item, optional: true
+
+ validates :name, presence: true, length: { maximum: 100 }
+ validates :quantity, numericality: { only_integer: true, greater_than: 0 }
+ validates :unit_price, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
+ validates :tax_rate, inclusion: { in: Item::TAX_RATES }
+
+ # 金額(税抜)= 数量 × 単価
+ def amount = quantity * unit_price
end
belongs_to :item, optional: true は「品目マスタとの紐づけは無くてもよい」という意味です。マスタに無い作業を、その場で書き足せるようにしています。数量は今回は整数に限定しており、「0.5人月」のような小数の数量は扱いません(必要になったら第4回の計算とあわせて見直します)。
3. 画面から親と子を一緒に受け取る(コントローラー)
app/controllers/estimates_controller.rb の要点は、受け取る項目(Strong Parameters)の書き方です。第1回と同じくHTMLだけに絞り、作成画面では明細を1行用意しておきます。生成された app/controllers/estimates_controller.rb に次の変更を加えます(+ が追加、- が削除)。
app/controllers/estimates_controller.rb の差分(90行)
--- a/app/controllers/estimates_controller.rb
+++ b/app/controllers/estimates_controller.rb
@@ -1,70 +1,53 @@
class EstimatesController < ApplicationController
+ require_permission :can_edit?, except: %i[ index show ]
+ require_permission :can_destroy?, only: :destroy
before_action :set_estimate, only: %i[ show edit update destroy ]
- # GET /estimates or /estimates.json
def index
- @estimates = Estimate.all
+ @estimates = Estimate.includes(:customer, :lines).order(issued_on: :desc, id: :desc)
end
- # GET /estimates/1 or /estimates/1.json
def show
end
- # GET /estimates/new
def new
- @estimate = Estimate.new
+ @estimate = Estimate.new(issued_on: Date.current, valid_until: Date.current + 30)
+ @estimate.lines.build
end
- # GET /estimates/1/edit
def edit
end
- # POST /estimates or /estimates.json
def create
@estimate = Estimate.new(estimate_params)
- respond_to do |format|
- if @estimate.save
- format.html { redirect_to @estimate, notice: "Estimate was successfully created." }
- format.json { render :show, status: :created, location: @estimate }
- else
- format.html { render :new, status: :unprocessable_content }
- format.json { render json: @estimate.errors, status: :unprocessable_content }
- end
+ if @estimate.save
+ redirect_to @estimate, notice: "#{Estimate.model_name.human}を作成しました。"
+ else
+ render :new, status: :unprocessable_content
end
end
- # PATCH/PUT /estimates/1 or /estimates/1.json
def update
- respond_to do |format|
- if @estimate.update(estimate_params)
- format.html { redirect_to @estimate, notice: "Estimate was successfully updated.", status: :see_other }
- format.json { render :show, status: :ok, location: @estimate }
- else
- format.html { render :edit, status: :unprocessable_content }
- format.json { render json: @estimate.errors, status: :unprocessable_content }
- end
+ if @estimate.update(estimate_params)
+ redirect_to @estimate, notice: "#{Estimate.model_name.human}を更新しました。", status: :see_other
+ else
+ render :edit, status: :unprocessable_content
end
end
- # DELETE /estimates/1 or /estimates/1.json
def destroy
@estimate.destroy!
-
- respond_to do |format|
- format.html { redirect_to estimates_path, notice: "Estimate was successfully destroyed.", status: :see_other }
- format.json { head :no_content }
- end
+ redirect_to estimates_path, notice: "#{Estimate.model_name.human}を削除しました。", status: :see_other
end
private
- # Use callbacks to share common setup or constraints between actions.
def set_estimate
- @estimate = Estimate.find(params.expect(:id))
+ @estimate = Estimate.includes(:lines).find(params.expect(:id))
end
- # Only allow a list of trusted parameters through.
def estimate_params
- params.expect(estimate: [ :customer_id, :number, :title, :issued_on, :valid_until, :note ])
+ params.expect(estimate: [ :customer_id, :title, :issued_on, :valid_until, :note,
+ lines_attributes: [ [ :id, :item_id, :position, :name, :unit, :quantity, :unit_price, :tax_rate, :_destroy ] ] ])
end
end
lines_attributes の中に、明細1行ぶんの項目を並べています。_destroy は「この行を削除する」という合図で、id は「保存済みのどの行か」を表します。
権限は第2回の仕組みをそのまま使い、先頭の2行(require_permission)で済ませています。
閲覧のみのユーザーは一覧と詳細を見るだけ、担当者は作成・編集まで、削除は管理者だけ、という線引きが、品目・取引先と同じ書き方で揃います。
scaffold が足したルートは、ほかのマスタの後ろに並べ替えておきます。config/routes.rb に次の変更を加えます(+ が追加、- が削除)。
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -1,5 +1,4 @@
Rails.application.routes.draw do
- resources :estimates
resource :session
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
@@ -16,4 +15,5 @@ Rails.application.routes.draw do
resources :customers
resources :items
+ resources :estimates
end
4. 入れ子のフォームを作る
生成された app/views/estimates/_form.html.erb を、明細の入れ子を持つフォームに直します。中心は明細の部分(data-controller="nested-form" の中)です。app/views/estimates/_form.html.erb に次の変更を加えます(+ が追加、- が削除)。
app/views/estimates/_form.html.erb の差分(90行)
--- a/app/views/estimates/_form.html.erb
+++ b/app/views/estimates/_form.html.erb
@@ -1,47 +1,53 @@
<%= form_with(model: estimate) do |form| %>
- <% if estimate.errors.any? %>
- <div style="color: red">
- <h2><%= pluralize(estimate.errors.count, "error") %> prohibited this estimate from being saved:</h2>
+ <%= render "shared/errors", record: estimate %>
- <ul>
- <% estimate.errors.each do |error| %>
- <li><%= error.full_message %></li>
- <% end %>
- </ul>
- </div>
- <% end %>
-
- <div>
- <%= form.label :customer_id, style: "display: block" %>
- <%= form.text_field :customer_id %>
+ <div class="field">
+ <%= form.label :customer_id %>
+ <%= form.collection_select :customer_id, Customer.order(:name), :id, :name, { prompt: "選んでください" }, required: true %>
</div>
-
- <div>
- <%= form.label :number, style: "display: block" %>
- <%= form.text_field :number %>
+ <div class="field">
+ <%= form.label :title %>
+ <%= form.text_field :title, required: true %>
</div>
-
- <div>
- <%= form.label :title, style: "display: block" %>
- <%= form.text_field :title %>
+ <div class="field">
+ <%= form.label :issued_on %>
+ <%= form.date_field :issued_on, required: true %>
</div>
-
- <div>
- <%= form.label :issued_on, style: "display: block" %>
- <%= form.date_field :issued_on %>
+ <div class="field">
+ <%= form.label :valid_until %>
+ <%= form.date_field :valid_until %>
</div>
- <div>
- <%= form.label :valid_until, style: "display: block" %>
- <%= form.date_field :valid_until %>
+ <h2>明細</h2>
+ <div data-controller="nested-form">
+ <table class="lines">
+ <thead>
+ <tr><th>品名</th><th>数量</th><th>単位</th><th>単価(税抜)</th><th>税率</th><th></th></tr>
+ </thead>
+ <tbody data-nested-form-target="container">
+ <%= form.fields_for :lines do |line_form| %>
+ <%= render "line_fields", form: line_form %>
+ <% end %>
+ </tbody>
+ </table>
+
+ <%# 行の追加用ひな形。NEW_RECORD は JavaScript が一意の数字に置き換える %>
+ <template data-nested-form-target="template">
+ <%= form.fields_for :lines, EstimateLine.new, child_index: "NEW_RECORD" do |line_form| %>
+ <%= render "line_fields", form: line_form %>
+ <% end %>
+ </template>
+
+ <p><button type="button" data-action="nested-form#add">明細行を追加</button></p>
</div>
- <div>
- <%= form.label :note, style: "display: block" %>
- <%= form.textarea :note %>
+ <div class="field">
+ <%= form.label :note %>
+ <%= form.text_area :note, rows: 3 %>
</div>
- <div>
- <%= form.submit %>
+ <div class="actions">
+ <%= form.submit "保存する" %>
+ <%= link_to "一覧へ戻る", estimates_path %>
</div>
<% end %>
fields_for :lines が、明細1行ぶんの入力欄を作ります。行の追加は「ひな形(<template>)を複製する」方式にしました。ひな形の中では、行の番号の代わりに NEW_RECORD という目印を入れておき、追加のたびにJavaScriptが重ならない数字へ置き換えます。
1行ぶんの入力欄は app/views/estimates/_line_fields.html.erb に切り出します。このファイルを新しく作ります。
<%# app/views/estimates/_line_fields.html.erb %>
<tr data-nested-form-row data-new-record="<%= form.object.new_record? %>">
<%= form.hidden_field :id if form.object.persisted? %>
<%= form.hidden_field :_destroy %>
<td><%= form.text_field :name, required: true, placeholder: "品名" %></td>
<td><%= form.number_field :quantity, min: 1, step: 1, class: "short" %></td>
<td><%= form.text_field :unit, class: "short" %></td>
<td><%= form.number_field :unit_price, min: 0, step: 1 %></td>
<td><%= form.select :tax_rate, Item::TAX_RATES.map { |r| [ "#{r}%", r ] } %></td>
<td><button type="button" data-action="nested-form#remove">削除</button></td>
</tr>
5. 明細行を追加・削除する小さなJavaScript(Stimulus)
ここで使うのが、Rails 8に標準で入っている Hotwire(ホットワイヤー) の一部、Stimulus(スティミュラス) です。Hotwireは「大きなJavaScriptの画面部品を作らず、サーバーが作ったHTMLを活かしながら、必要な所だけ動きを足す」ためのRailsの標準的な仕組みで、Stimulusはその「必要な所だけ動きを足す」担当です。HTMLに data-controller のような印を付けておくと、対応するJavaScriptが動きます(公式の解説は下記のStimulus Handbookにあります)。
📰 出典:Stimulus Handbook(Hotwire公式)
app/javascript/controllers/nested_form_controller.js を新しく作ります。全体は次のとおりです。
// app/javascript/controllers/nested_form_controller.js
import { Controller } from "@hotwired/stimulus"
// 明細行の追加・削除。<template> の中身を複製し、NEW_RECORD を一意の数字に置き換えて追加する
export default class extends Controller {
static targets = [ "template", "container" ]
add(event) {
event.preventDefault()
const id = Date.now().toString()
this.containerTarget.insertAdjacentHTML("beforeend", this.templateTarget.innerHTML.replaceAll("NEW_RECORD", id))
}
remove(event) {
event.preventDefault()
const row = event.target.closest("[data-nested-form-row]")
if (row.dataset.newRecord === "true") {
// まだ保存していない行は、画面から消すだけでよい
row.remove()
} else {
// 保存済みの行は _destroy を立てて隠す(サーバー側で削除される)
row.querySelector("input[name$='[_destroy]']").value = "1"
row.hidden = true
}
}
}
20行ほどです。ファイルを controllers/ に置くだけで、index.js が自動で読み込みます(第0回の雛形の仕組みです)。
削除が2通りに分かれる理由は重要です。まだ保存していない行は、画面から消せば終わりです。一方、すでに保存済みの行は、画面から消すだけではサーバーに伝わらず、保存しても行が残ってしまいます。そこで _destroy に 1 を入れて隠し、保存したときにサーバー側で削除する形にしています。
なお、今回の動きは「ブラウザの中で行を足す」までで、サーバーとの通信は保存ボタンを押したときの1回だけです。Hotwireにはもう一つの柱のTurbo(画面の一部だけをサーバーのHTMLで書き換える仕組み)があり、金額の自動集計などで第4回以降に活躍します。
6. 一覧・詳細と、ほかの画面への影響
一覧(index)と詳細(show)では、各明細の「金額(税抜)=数量×単価」と、見積書の小計を表示します。税額と合計の計算は、端数処理が絡むため第4回でまとめて扱います。
生成された一覧・詳細・作成・編集の画面を、次のように直します。
app/views/estimates/index.html.erb に次の変更を加えます(+ が追加、- が削除)。
app/views/estimates/index.html.erb の差分(40行)
--- a/app/views/estimates/index.html.erb
+++ b/app/views/estimates/index.html.erb
@@ -1,16 +1,25 @@
-<p style="color: green"><%= notice %></p>
+<% content_for :title, "見積書一覧" %>
-<% content_for :title, "Estimates" %>
+<h1>見積書</h1>
-<h1>Estimates</h1>
-
-<div id="estimates">
- <% @estimates.each do |estimate| %>
- <%= render estimate %>
- <p>
- <%= link_to "Show this estimate", estimate %>
- </p>
- <% end %>
+<div class="actions">
+ <% if Current.user.can_edit? %><%= link_to "見積書を作成", new_estimate_path %><% end %>
</div>
-<%= link_to "New estimate", new_estimate_path %>
+<table>
+ <thead>
+ <tr><th>見積番号</th><th>件名</th><th>取引先</th><th>発行日</th><th class="num">小計(税抜)</th></tr>
+ </thead>
+ <tbody>
+ <% @estimates.each do |estimate| %>
+ <tr>
+ <td><%= link_to estimate.number, estimate %></td>
+ <td><%= estimate.title %></td>
+ <td><%= estimate.customer.name %></td>
+ <td><%= estimate.issued_on.strftime("%Y/%m/%d") %></td>
+ <td class="num"><%= number_with_delimiter(estimate.subtotal) %>円</td>
+ </tr>
+ <% end %>
+ </tbody>
+</table>
+<% if @estimates.empty? %><p class="muted">見積書はまだありません。</p><% end %>
app/views/estimates/show.html.erb に次の変更を加えます(+ が追加、- が削除)。
app/views/estimates/show.html.erb の差分(49行)
--- a/app/views/estimates/show.html.erb
+++ b/app/views/estimates/show.html.erb
@@ -1,10 +1,40 @@
-<p style="color: green"><%= notice %></p>
+<% content_for :title, @estimate.number %>
+<h1><%= @estimate.title %></h1>
-<%= render @estimate %>
+<dl>
+ <dt><%= Estimate.human_attribute_name(:number) %></dt><dd><%= @estimate.number %></dd>
+ <dt><%= Estimate.human_attribute_name(:customer_id) %></dt><dd><%= link_to @estimate.customer.name, @estimate.customer %></dd>
+ <dt><%= Estimate.human_attribute_name(:issued_on) %></dt><dd><%= @estimate.issued_on.strftime("%Y/%m/%d") %></dd>
+ <dt><%= Estimate.human_attribute_name(:valid_until) %></dt><dd><%= @estimate.valid_until&.strftime("%Y/%m/%d") || "—" %></dd>
+</dl>
-<div>
- <%= link_to "Edit this estimate", edit_estimate_path(@estimate) %> |
- <%= link_to "Back to estimates", estimates_path %>
+<table>
+ <thead>
+ <tr><th>品名</th><th class="num">数量</th><th>単位</th><th class="num">単価</th><th>税率</th><th class="num">金額(税抜)</th></tr>
+ </thead>
+ <tbody>
+ <% @estimate.lines.each do |line| %>
+ <tr>
+ <td><%= line.name %></td>
+ <td class="num"><%= line.quantity %></td>
+ <td><%= line.unit %></td>
+ <td class="num"><%= number_with_delimiter(line.unit_price) %></td>
+ <td><%= line.tax_rate %>%</td>
+ <td class="num"><%= number_with_delimiter(line.amount) %></td>
+ </tr>
+ <% end %>
+ </tbody>
+ <tfoot>
+ <tr><th colspan="5" class="num">小計(税抜)</th><td class="num"><%= number_with_delimiter(@estimate.subtotal) %>円</td></tr>
+ </tfoot>
+</table>
- <%= button_to "Destroy this estimate", @estimate, method: :delete %>
+<% if @estimate.note.present? %><p><%= simple_format(h(@estimate.note)) %></p><% end %>
+
+<div class="actions">
+ <%= link_to "編集", edit_estimate_path(@estimate) if Current.user.can_edit? %>
+ <%= link_to "一覧へ戻る", estimates_path %>
+ <% if Current.user.can_destroy? %>
+ <%= button_to "削除", @estimate, method: :delete, form: { data: { turbo_confirm: "この見積書を削除しますか?" } } %>
+ <% end %>
</div>
app/views/estimates/new.html.erb に次の変更を加えます(+ が追加、- が削除)。
--- a/app/views/estimates/new.html.erb
+++ b/app/views/estimates/new.html.erb
@@ -1,11 +1,3 @@
-<% content_for :title, "New estimate" %>
-
-<h1>New estimate</h1>
-
+<% content_for :title, "見積書の作成" %>
+<h1>見積書の作成</h1>
<%= render "form", estimate: @estimate %>
-
-<br>
-
-<div>
- <%= link_to "Back to estimates", estimates_path %>
-</div>
app/views/estimates/edit.html.erb に次の変更を加えます(+ が追加、- が削除)。
--- a/app/views/estimates/edit.html.erb
+++ b/app/views/estimates/edit.html.erb
@@ -1,12 +1,3 @@
-<% content_for :title, "Editing estimate" %>
-
-<h1>Editing estimate</h1>
-
+<% content_for :title, "見積書の編集" %>
+<h1>見積書の編集(<%= @estimate.number %>)</h1>
<%= render "form", estimate: @estimate %>
-
-<br>
-
-<div>
- <%= link_to "Show this estimate", @estimate %> |
- <%= link_to "Back to estimates", estimates_path %>
-</div>
ヘッダーのメニュー、明細の入力欄の幅、項目名の日本語も足します。
app/views/layouts/application.html.erb に次の変更を加えます(+ が追加、- が削除)。
--- a/app/views/layouts/application.html.erb
+++ b/app/views/layouts/application.html.erb
@@ -23,6 +23,7 @@
<nav>
<%= link_to "取引先", customers_path %>
<%= link_to "品目", items_path %>
+ <%= link_to "見積書", estimates_path %>
</nav>
<div class="account">
<%= Current.user.email_address %>(<%= User.human_attribute_name("role.#{Current.user.role}") %>)
app/assets/stylesheets/application.css に次の変更を加えます(+ が追加、- が削除)。
--- a/app/assets/stylesheets/application.css
+++ b/app/assets/stylesheets/application.css
@@ -18,3 +18,5 @@ td.num, th.num { text-align: right; }
.login-form { max-width: 24rem; }
.login-form label { display: block; font-weight: bold; }
.login-form input[type=email], .login-form input[type=password] { width: 100%; }
+table.lines input[type=text], table.lines input[type=number], table.lines select { width: 100%; box-sizing: border-box; padding: .3rem; }
+table.lines input.short { width: 4.5rem; }
config/locales/ja.yml に次の変更を加えます(+ が追加、- が削除)。
--- a/config/locales/ja.yml
+++ b/config/locales/ja.yml
@@ -3,6 +3,8 @@ ja:
models:
customer: 取引先
item: 品目
+ estimate: 見積書
+ estimate_line: 見積明細
attributes:
customer:
name: 取引先名
@@ -27,6 +29,20 @@ ja:
unit_price: 単価(税抜)
tax_rate: 消費税率
active: 使用中
+ estimate:
+ customer_id: 取引先
+ number: 見積番号
+ title: 件名
+ issued_on: 発行日
+ valid_until: 有効期限
+ note: 備考
+ lines: 明細
+ estimate_line:
+ name: 品名
+ quantity: 数量
+ unit: 単位
+ unit_price: 単価(税抜)
+ tax_rate: 消費税率
errors:
format: "%{attribute}%{message}"
messages:
@@ -38,4 +54,5 @@ ja:
not_a_number: は数字で入力してください
not_an_integer: は整数で入力してください
greater_than_or_equal_to: は%{count}以上にしてください
+ greater_than: は%{count}より大きくしてください
too_long: は%{count}文字以内にしてください
もう一点、見積書ができると、取引先・品目の削除の扱いが変わります。開発中にテストが失敗して気づいた点です。
- 見積書がある取引先を消すと、見積書の宛先が無くなってしまう。そこで
has_many :estimates, dependent: :restrict_with_errorを取引先に足し、見積書がある取引先は削除できないようにしました(画面には「見積書がある取引先は削除できません」と出ます) - 品目は、削除しても明細に品名・単価を写してあるので、明細は残して紐づけだけ外します(
dependent: :nullify)
app/models/customer.rb に次の変更を加えます(+ が追加、- が削除)。
--- a/app/models/customer.rb
+++ b/app/models/customer.rb
@@ -1,4 +1,7 @@
class Customer < ApplicationRecord
+ # 見積書がある取引先は削除できない(見積の記録を残すため)
+ has_many :estimates, dependent: :restrict_with_error
+
validates :name, presence: true, length: { maximum: 100 }
validates :postal_code, format: { with: /\A\d{3}-?\d{4}\z/ }, allow_blank: true
validates :email, format: { with: URI::MailTo::EMAIL_REGEXP }, allow_blank: true
app/controllers/customers_controller.rb に次の変更を加えます(+ が追加、- が削除)。
--- a/app/controllers/customers_controller.rb
+++ b/app/controllers/customers_controller.rb
@@ -36,8 +36,11 @@ class CustomersController < ApplicationController
end
def destroy
- @customer.destroy!
- redirect_to customers_path, notice: "#{Customer.model_name.human}を削除しました。", status: :see_other
+ if @customer.destroy
+ redirect_to customers_path, notice: "#{Customer.model_name.human}を削除しました。", status: :see_other
+ else
+ redirect_to @customer, alert: "見積書がある取引先は削除できません。", status: :see_other
+ end
end
private
app/models/item.rb に次の変更を加えます(+ が追加、- が削除)。
--- a/app/models/item.rb
+++ b/app/models/item.rb
@@ -2,6 +2,9 @@ class Item < ApplicationRecord
# 消費税率(%)。軽減税率の8%と標準の10%だけを許す
TAX_RATES = [ 8, 10 ].freeze
+ # 見積明細は品名・単価を写して持つので、品目を削除しても明細は残す(紐づけだけ外す)
+ has_many :estimate_lines, dependent: :nullify
+
validates :code, presence: true, uniqueness: true, length: { maximum: 30 }
validates :name, presence: true, length: { maximum: 100 }
validates :unit_price, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
「消してよいデータ」と「記録として残すデータ」の線引きは、見積・請求のような帳票系のシステムでは最初に決めておきたい論点です。
動作確認:自動テストと、ブラウザでの操作確認
今回の機能は、次の観点を自動テストにしています。
- 明細の金額が「数量×単価」になり、小計が合計になる
- 保存すると見積番号(
Q-00001の形)が付く - 明細が1行もない見積書は保存できない
- 品名が空の行は無視される
- 数量が0の明細、発行日より前の有効期限は保存できない
- 見積書を削除すると明細も消える
- 閲覧のみの権限では作成できず、削除は管理者だけができる
- 編集画面で明細を削除できる
テスト用の見積書と明細(fixture)を置きます。test/fixtures/estimates.yml と test/fixtures/estimate_lines.yml を新しく作ります。
# test/fixtures/estimates.yml
website:
customer: sakura
number: Q-00001
title: コーポレートサイト制作
issued_on: 2026-10-01
valid_until: 2026-10-31
# test/fixtures/estimate_lines.yml
design:
estimate: website
item: design
position: 1
name: 画面デザイン費
unit: 式
quantity: 1
unit_price: 300000
tax_rate: 10
lunch:
estimate: website
position: 2
name: 打合せ用弁当
unit: 個
quantity: 5
unit_price: 1000
tax_rate: 8
test/models/estimate_test.rb と test/controllers/estimates_controller_test.rb を新しく作ります。
# test/models/estimate_test.rb
require "test_helper"
class EstimateTest < ActiveSupport::TestCase
def build_estimate(lines: [ { name: "設計費", quantity: 2, unit_price: 50000, tax_rate: 10 } ], **attrs)
Estimate.new({ customer: customers(:sakura), title: "テスト見積", issued_on: Date.new(2026, 10, 1), lines_attributes: lines }.merge(attrs))
end
test "明細の金額は数量×単価で、小計は金額の合計になる" do
assert_equal 5_000, estimates(:website).lines.find_by!(name: "打合せ用弁当").amount
assert_equal 305_000, estimates(:website).subtotal
end
test "保存すると見積番号が付く" do
estimate = build_estimate
estimate.save!
assert_equal format("Q-%05d", estimate.id), estimate.number
end
test "明細が1行もない見積書は保存できない" do
estimate = build_estimate(lines: [])
assert_not estimate.valid?
assert_includes estimate.errors.full_messages, "明細を1行以上入力してください"
end
test "品名が空の行は無視される" do
estimate = build_estimate(lines: [ { name: "設計費", quantity: 1, unit_price: 1000 }, { name: "", quantity: 1, unit_price: 0 } ])
estimate.save!
assert_equal 1, estimate.lines.count
end
test "数量は1以上の整数" do
estimate = build_estimate(lines: [ { name: "設計費", quantity: 0, unit_price: 1000 } ])
assert_not estimate.valid?
end
test "有効期限は発行日より前にできない" do
estimate = build_estimate(valid_until: Date.new(2026, 9, 1))
assert_not estimate.valid?
end
test "見積書を削除すると明細も消える" do
assert_difference "EstimateLine.count", -2 do
estimates(:website).destroy!
end
end
end
# test/controllers/estimates_controller_test.rb
require "test_helper"
class EstimatesControllerTest < ActionDispatch::IntegrationTest
def estimate_params(lines)
{ estimate: { customer_id: customers(:sakura).id, title: "保守契約", issued_on: "2026-10-02", lines_attributes: lines } }
end
test "一覧に小計(税抜)が桁区切りで表示される" do
sign_in_as users(:viewer)
get estimates_url
assert_response :success
assert_select "td.num", "305,000円"
end
test "詳細に明細と小計(税抜)が表示される" do
sign_in_as users(:viewer)
get estimate_url(estimates(:website))
assert_response :success
assert_select "tbody tr", 2
assert_select "tfoot td", "305,000円"
end
test "作成画面に明細行のひな形(template)がある" do
sign_in_as users(:staff)
get new_estimate_url
assert_response :success
assert_select "template[data-nested-form-target=template] input[name*='NEW_RECORD']"
end
test "明細付きで作成できる" do
sign_in_as users(:staff)
assert_difference [ "Estimate.count", "EstimateLine.count" ], 1 do
post estimates_url, params: estimate_params(
"0" => { name: "月額保守", quantity: 12, unit: "月", unit_price: 30000, tax_rate: 10 }
)
end
assert_redirected_to estimate_url(Estimate.order(:id).last)
end
test "明細がなければ作成できない" do
sign_in_as users(:staff)
assert_no_difference "Estimate.count" do
post estimates_url, params: estimate_params({})
end
assert_response :unprocessable_content
end
test "編集で明細を削除できる" do
sign_in_as users(:staff)
estimate = estimates(:website)
patch estimate_url(estimate), params: { estimate: { lines_attributes: { "0" => { id: estimate_lines(:lunch).id, _destroy: "1" } } } }
assert_redirected_to estimate_url(estimate)
assert_equal 300_000, estimate.reload.subtotal
end
test "閲覧のみの権限では作成できない" do
sign_in_as users(:viewer)
assert_no_difference "Estimate.count" do
post estimates_url, params: estimate_params("0" => { name: "x", quantity: 1, unit_price: 1 })
end
assert_redirected_to root_url
end
test "削除は管理者だけ" do
sign_in_as users(:staff)
assert_no_difference "Estimate.count" do
delete estimate_url(estimates(:website))
end
sign_in_as users(:admin)
assert_difference "Estimate.count", -1 do
delete estimate_url(estimates(:website))
end
end
end
取引先の削除の扱いが変わったので、test/controllers/customers_controller_test.rb にも次の変更を加えます(+ が追加、- が削除)。
--- a/test/controllers/customers_controller_test.rb
+++ b/test/controllers/customers_controller_test.rb
@@ -36,4 +36,11 @@ class CustomersControllerTest < ActionDispatch::IntegrationTest
delete customer_url(customers(:momiji))
end
end
+
+ test "見積書がある取引先は削除できない" do
+ assert_no_difference "Customer.count" do
+ delete customer_url(customers(:sakura))
+ end
+ assert_redirected_to customer_url(customers(:sakura))
+ end
end
検証は次のコマンドで行い、今回の変更後は46件のテストがすべて通り、RuboCop(コードの書き方の自動チェック)も指摘なしでした。
bin/rails db:migrate
bin/rails test
bin/rubocop
テストでは確かめられない「画面上で行を足す・消す」動きは、開発環境でブラウザ(Chromium)を自動操作して確認しました。具体的には、①ログイン後に作成画面を開くと明細が1行ある、②「明細行を追加」を2回押すと3行になる、③追加した行の「削除」で2行に戻る、④2行に入力して保存すると見積書の詳細画面に移る、までを確かめています(開発環境での確認です)。
この確認で、実際にひとつ不具合を見つけました。最初に書いた削除の処理は「_destroy の入力欄があれば隠す」という作りで、新しく追加した行にも同じ入力欄があるため、追加した行が画面から消えず「隠れただけ」になっていました。そのまま保存すると、隠れた行は品名が入力できないまま残ります。上のコードの data-new-record(新しい行かどうかの印)による分岐は、この修正です。画面の動きは、自動テストだけでは見逃しやすい部分でした。
つまずきやすい点・注意
_destroyとidの取りこぼし:受け取る項目(Strong Parameters)にidと_destroyを入れ忘れると、編集時に明細が「更新」ではなく「新しい行として追加」され、同じ明細が二重になります- 行番号の重複:追加する行の番号(
NEW_RECORDの置き換え先)は、他の行と重ならない値にします。今回は時刻のミリ秒を使っていますが、極めて短い間隔で連続追加すると重なる理論上の余地があります(人の操作では起きにくい水準です) - 並び順(
position):列は用意しましたが、画面での並べ替えは今回扱っていません。入力した順に保存されます - 「保存したあとに金額が変わらない」仕組みは別問題:品名・単価を明細に写して持つのはその第一歩ですが、見積書の「確定(発行済み)」状態で編集を止める仕組みは、請求書への変換(第7回)とあわせて扱います
- この連載で省略していること:見積書の複製、品目マスタからの選択入力、明細の並べ替え、小数の数量、値引き行は今回扱いません。必要な場合は、その分が別の工数になります
発注者向けメモ:見積書機能を頼むときの確認点
見積書の機能は「どこまで自動で作るか」で費用が大きく変わります。開発会社に頼むときは、次を確認してください。
- ☐ 今の見積書のExcelやPDFの現物を見せ、「この項目は必須」「この欄は空でもよい」を分けたか
- ☐ 明細の数量に小数(例:0.5人月)や、値引き・「一式」の行が必要か伝えたか
- ☐ 見積番号のルール(年度ごとの連番、取引先ごとの記号など)を決めているか
- ☐ 見積書を出したあとの修正を認めるか(認めるなら、改版の履歴を残すか)
- ☐ 品目マスタの単価を変えたとき、過去の見積書は変わらない前提で合意できているか
- ☐ 取引先や品目を削除したい要望が出たとき、過去の見積書をどう扱うか決めているか
開発会社への質問例
- 「明細の行数に上限はありますか。100行を超える見積書でも画面は問題なく動きますか」
- 「見積書を発行したあとに内容を直す場合、元の見積書はどう残りますか。改版の履歴は取れますか」
- 「見積番号のルール(年度・連番・取引先記号)は、あとから変えられますか。変えたとき、過去の番号はどうなりますか」
- 「品目マスタの価格を改定しても、すでに出した見積書の金額が変わらないことは、どう保証されていますか」
工数が増えやすいのは、小数の数量・値引きや「出精値引き」などの特殊な行、見積書の改版管理、承認フロー(上長の承認を経てから発行する)、そして既存のExcel見積書とそっくり同じ見た目での出力です。最初は「入力して、保存して、一覧で探せる」ところまでを小さく作り、使いながら足していく進め方が現実的です。
「明細の追加くらい簡単だと思っていましたが、『あとから金額が変わらないこと』や『消してよいデータの線引き』のほうが、先に決めておくべき大事な話なんですね。」
まとめと次回予告
第3回では、見積書(親)と明細行(子)をRails 8で作り、Stimulusの20行ほどのJavaScriptで明細行をその場で追加・削除できる画面を作りました。品名・単価を明細に写して持つこと、見積書がある取引先は消せないことなど、帳票系のシステムで最初に決めておきたい線引きも入れています。
次回の第4回は「金額・消費税・端数処理を正しく計算する」です。税率ごとの集計と端数処理の考え方を整理し、Minitestで計算結果を固めます。見積・請求システムで最も「間違えてはいけない」部分です。
この連載の記事一覧
この記事は連載「Railsで作る見積・請求管理システム」の1回です。連載のほかの回は次のとおりです(連載の一覧ページ)。
- 【Railsで作る見積・請求管理システム 第0回】Rails 8の雛形を動かして、Excel見積からの脱却を始める
- 【Railsで作る見積・請求管理システム 第1回】取引先と品目のマスタを作る(scaffoldと入力チェック)
- 【Railsで作る見積・請求管理システム 第2回】ログインを付ける(Rails 8標準の認証と権限の分け方)
- 【Railsで作る見積・請求管理システム 第3回】見積書と明細を作る(Hotwireで明細行をその場で追加)(この記事)
- 【Railsで作る見積・請求管理システム 第4回】金額・消費税・端数処理を正しく計算する(税率ごとに1回だけ丸める)
- 【Railsで作る見積・請求管理システム 第5回】見積書をPDFで出力する(Prawnで日本語フォントを扱う)
- 【Railsで作る見積・請求管理システム 第6回】見積書をメールで送る(Solid Queueで送信を非同期にする)
- 【Railsで作る見積・請求管理システム 第7回】受注から請求書へ変換する(状態遷移と請求番号、インボイスの記載事項)
- 【Railsで作る見積・請求管理システム 第8回】入金管理と督促の一覧を作る(一部入金・期限超過の抽出)
- 【Railsで作る見積・請求管理システム 第9回】Kamalでサーバーに配備する(Dockerイメージ・SSL・秘密情報・運用の注意)










コメント
コメント一覧 (5件)
[…] 前回の第3回:見積書と明細を作るでは、見積書(親)と明細行(子)を作り、画面で明細を追加・削除できるようにしました。ただし金額は「税抜の小計」だけで、消費税はまだ計算していませんでした。 […]
[…] […]
[…] […]
[…] […]
[…] […]