fix(transactions): re-render form when creating with no account#2777
fix(transactions): re-render form when creating with no account#2777pro3958 wants to merge 1 commit into
Conversation
… account 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 we-promise#2566
|
The 404 came from |
📝 WalkthroughWalkthrough
ChangesTransaction creation validation
Estimated code review effort: 2 (Simple) | ~10 minutes Possibly related PRs
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
test/controllers/transactions_controller_test.rb (1)
38-57: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick winAssert that the transaction form was rendered.
A 422 response alone would also pass if the controller returned a generic error page. Add a form-specific selector to protect the re-render contract.
Suggested assertion
assert_response :unprocessable_entity + assert_select "form[action=?]", transactions_path🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@test/controllers/transactions_controller_test.rb` around lines 38 - 57, Extend the “create without an account” test after the 422 assertion to verify that the transaction form was rendered, using a form-specific selector targeting the expected transaction form. Keep the existing no-persistence and response-status assertions unchanged.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@test/controllers/transactions_controller_test.rb`:
- Around line 38-57: Extend the “create without an account” test after the 422
assertion to verify that the transaction form was rendered, using a
form-specific selector targeting the expected transaction form. Keep the
existing no-persistence and response-status assertions unchanged.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: 012821a4-1840-4031-ae2c-8c65c9de5018
📒 Files selected for processing (2)
app/controllers/transactions_controller.rbtest/controllers/transactions_controller_test.rb
ReviewWhat it does: Correctness: Verified this holds up.
Test coverage: The added test is a real regression test, not tautological — tracing the old code path confirms it would 404 (not 422) before this fix, since Minor, non-blocking (already self-flagged in the PR description, and accurate): duplicated render logic between the new nil-branch and the existing failure Looks correct and well-scoped. Generated by Claude Code |
Summary
TransactionsController#createresolved the account withCurrent.user.accessible_accounts.find(params.dig(:entry, :account_id)). When no account is selected the id is blank,findraisesActiveRecord::RecordNotFound, andStoreLocation'srescue_fromrenders it ashead :not_found— the 404 on/transactionsreported here, with the submit button appearing to do nothing.This switches the lookup to
find_by(id:). When it returnsnil, the action rebuilds the entry, runsvalid?, callsset_new_transaction_form_options, and re-renders:newwith422, mirroring the existing validation-failure branch. That handles a blank, absent, or otherwise invalidaccount_idthe same way, so the user gets the form back with an error instead of a broken request.Verification
bin/rails test test/controllers/transactions_controller_test.rbpasses. The added test posts a transaction with a blankaccount_id, asserts noEntry/Transactionis created, and expects a422— it gets a 404 on the unfixed controller.Fixes #2566
Notes I made while reviewing this
app/controllers/transactions_controller.rb:101— The nil branch duplicates the failure-render logic already present in the else branch (set_new_transaction_form_options + render :new, status: :unprocessable_entity). Minor duplication.test/controllers/transactions_controller_test.rb:55— The test asserts status 422 and no record creation, but does not assert that the re-rendered form actually shows the 'Account must exist' validation error. That distinction (422-with-visible-error vs. 422-with-silent-blank-form) is the real user-facing improvement and isn't locked down.app/controllers/valuations_controller.rb:50— Identical latent bug: valuations#create uses accessible_accounts.find(params.dig(:entry, :account_id)), which will 404 the same way when no account is selected. Not touched by this diff.test/controllers/transactions_controller_test.rb:51— Test usesCategory.first.idrather than a named fixture reference (e.g.categories(:food_and_drink)), a soft deviation from the repo's fixtures convention. Non-blocking, andCategory.firstis only incidental to the code path under test (the entry never persists), but it makes the test order-dependent.app/controllers/transactions_controller.rb:101— The CLAUDE.md pre-PR gates (bin/rails test, rubocop, brakeman, npm lint, importmap audit) could not be executed in this review environment (no Ruby toolchain present), so gate-passing is asserted statically, not demonstrated. The commit/PR must also link the issue withfixes #2566.Summary by CodeRabbit
Bug Fixes
Tests