From fcb46be19fef155b222ea2fd7391ecfcb0e344c1 Mon Sep 17 00:00:00 2001 From: pro3958 Date: Fri, 21 Aug 2026 21:31:18 -0700 Subject: [PATCH] fix(transactions): re-render form when creating a transaction with no account (#2777) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- app/controllers/transactions_controller.rb | 10 ++++++++- .../transactions_controller_test.rb | 21 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/app/controllers/transactions_controller.rb b/app/controllers/transactions_controller.rb index 5d3ef24ac..0a7a53b67 100644 --- a/app/controllers/transactions_controller.rb +++ b/app/controllers/transactions_controller.rb @@ -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) diff --git a/test/controllers/transactions_controller_test.rb b/test/controllers/transactions_controller_test.rb index 845cba260..4fed721ff 100644 --- a/test/controllers/transactions_controller_test.rb +++ b/test/controllers/transactions_controller_test.rb @@ -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: {