From 166ed4b1ea53ac1fcc141411d1e87d25905db2e2 Mon Sep 17 00:00:00 2001 From: Zach Gollwitzer Date: Mon, 26 Aug 2024 19:10:17 -0400 Subject: [PATCH] Fix account transaction form resetting amount to 0 (#1133) --- .../account/transactions_controller.rb | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/app/controllers/account/transactions_controller.rb b/app/controllers/account/transactions_controller.rb index 61571cb4..0a0d88a2 100644 --- a/app/controllers/account/transactions_controller.rb +++ b/app/controllers/account/transactions_controller.rb @@ -12,8 +12,7 @@ class Account::TransactionsController < ApplicationController end def update - @entry.update!(entry_params.merge(amount: amount)) - @entry.sync_account_later + @entry.update!(entry_params) respond_to do |format| format.html { redirect_to account_entry_path(@account, @entry), notice: t(".success") } @@ -34,7 +33,7 @@ class Account::TransactionsController < ApplicationController def entry_params params.require(:account_entry) .permit( - :name, :date, :amount, :currency, :entryable_type, + :name, :date, :amount, :currency, :entryable_type, :nature, entryable_attributes: [ :id, :notes, @@ -43,14 +42,18 @@ class Account::TransactionsController < ApplicationController :merchant_id, { tag_ids: [] } ] - ) - end + ).tap do |permitted_params| + nature = permitted_params.delete(:nature) - def amount - if params[:account_entry][:nature] == "income" - entry_params[:amount].to_d * -1 - else - entry_params[:amount].to_d - end + if permitted_params[:amount] + amount_value = permitted_params[:amount].to_d + + if nature == "income" + amount_value *= -1 + end + + permitted_params[:amount] = amount_value + end + end end end