fix(transactions): re-render form when creating a transaction with no account (#2777)

TransactionsController#create looked up the account with
accessible_accounts.find(params.dig(:entry, :account_id)). With no
account selected the id is blank, find raises RecordNotFound, and
StoreLocation's rescue_from turns that into head :not_found — the 404
on /transactions the reporter saw.

Switch to find_by(id:) and, when it returns nil, rebuild the entry,
run validation, and re-render :new with 422, matching the existing
validation-failure branch. This covers a blank, missing, or invalid
account_id, so the user gets the form back with errors instead of a
dead button.

Fixes #2566

Co-authored-by: agentloop <agentloop@localhost>
This commit is contained in:
pro3958
2026-08-22 06:31:18 +02:00
committed by GitHub
co-authored by agentloop
parent b0ecb919b0
commit fcb46be19f
2 changed files with 30 additions and 1 deletions
+9 -1
View File
@@ -117,7 +117,15 @@ class TransactionsController < ApplicationController
end
def create
account = Current.user.accessible_accounts.find(params.dig(:entry, :account_id))
account = Current.user.accessible_accounts.find_by(id: params.dig(:entry, :account_id))
if account.nil?
@entry = Current.family.entries.new(entry_params)
@entry.valid?
set_new_transaction_form_options
render :new, status: :unprocessable_entity
return
end
return unless require_account_permission!(account)
@@ -35,6 +35,27 @@ class TransactionsControllerTest < ActionDispatch::IntegrationTest
assert_enqueued_with(job: SyncJob)
end
test "create without an account re-renders the form instead of raising" do
assert_no_difference [ "Entry.count", "Transaction.count" ] do
post transactions_url, params: {
entry: {
account_id: "",
name: "New transaction",
date: Date.current,
currency: "USD",
amount: 100,
nature: "inflow",
entryable_type: "Transaction",
entryable_attributes: {
category_id: Category.first.id
}
}
}
end
assert_response :unprocessable_entity
end
test "updates with transaction details" do
assert_no_difference [ "Entry.count", "Transaction.count" ] do
patch transaction_url(@entry), params: {