fix(goals): default goal currency so it survives a failed create (#2171)

When a new goal failed server-side validation (e.g. no funding account
selected), the currency was never assigned — `goal_params` doesn't permit
`:currency` and it was only derived from the first linked account. On the
re-rendered form the money field fell back to its USD default, so a CHF
(or any non-USD) family saw their target amount silently reset to dollars,
and a spurious "Currency must be filled" error appeared on a field the
user can't even set.

Always default the currency to the first linked account's currency when
present, otherwise the family's primary currency. Currency is now never
blank, so the derived-field error never surfaces and the money field keeps
the family currency across a failed submit.

Fixes we-promise/sure#2170
This commit is contained in:
Guillem Arias Fauste
2026-06-04 11:57:16 +02:00
committed by GitHub
parent 1742f4ef1e
commit 6a89efb9c9

View File

@@ -61,7 +61,7 @@ class GoalsController < ApplicationController
def create
@goal = Current.family.goals.new(goal_params)
accounts = lookup_accounts(params.dig(:goal, :account_ids))
@goal.currency = accounts.first.currency if accounts.any? && @goal.currency.blank?
@goal.currency = (accounts.first&.currency || Current.family.primary_currency_code) if @goal.currency.blank?
Goal.transaction do
accounts.each { |a| @goal.goal_accounts.build(account: a) }