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: {