The personal finance app for everyone
Find account first and build transaction through association (#487)
Dwight Watson971347c761f5
diff --git a/app/controllers/transactions_controller.rb b/app/controllers/transactions_controller.rbindex 3a562445..abc05a0b 100644--- a/app/controllers/transactions_controller.rb+++ b/app/controllers/transactions_controller.rb@@ -17,12 +17,9 @@ class TransactionsController < ApplicationControllerenddef create- @transaction = Transaction.new(transaction_params)account = Current.family.accounts.find(params[:transaction][:account_id])- raise ActiveRecord::RecordNotFound, "Account not found or not accessible" if account.nil?-- @transaction.account = account+ @transaction = account.transactions.build(transaction_params)respond_to do |format|if @transaction.savediff --git a/test/controllers/transactions_controller_test.rb b/test/controllers/transactions_controller_test.rbindex eb3eccdc..addd3364 100644--- a/test/controllers/transactions_controller_test.rb+++ b/test/controllers/transactions_controller_test.rb@@ -17,11 +17,12 @@ class TransactionsControllerTest < ActionDispatch::IntegrationTestendtest "should create transaction" do+ name = "transaction_name"assert_difference("Transaction.count") do- post transactions_url, params: { transaction: { account_id: @transaction.account_id, amount: @transaction.amount, currency: @transaction.currency, date: @transaction.date, name: @transaction.name } }+ post transactions_url, params: { transaction: { account_id: @transaction.account_id, amount: @transaction.amount, currency: @transaction.currency, date: @transaction.date, name: } }end- assert_redirected_to transaction_url(Transaction.last)+ assert_redirected_to transaction_url(Transaction.find_by(name:))endtest "should show transaction" do