Skip to content

armgit / maybe

Public Imported from GitHub
0

The personal finance app for everyone

Add error handling for AccountsController#create (#957)

Tony Vincent3ffb6cb62be1
diff --git a/app/controllers/accounts_controller.rb b/app/controllers/accounts_controller.rb
index 4633d8e9..8544b611 100644
--- a/app/controllers/accounts_controller.rb
+++ b/app/controllers/accounts_controller.rb
@@ -3,7 +3,6 @@ class AccountsController < ApplicationController
include Filterable
before_action :set_account, only: %i[ edit show destroy sync update ]
- after_action :sync_account, only: :create
def index
@institutions = Current.family.institutions
@@ -52,8 +51,10 @@ class AccountsController < ApplicationController
attributes: account_params.except(:start_date, :start_balance),
start_date: account_params[:start_date],
start_balance: account_params[:start_balance]
-
+ @account.sync_later
redirect_back_or_to account_path(@account), notice: t(".success")
+ rescue ActiveRecord::RecordInvalid => e
+ redirect_back_or_to accounts_path, alert: e.record.errors.full_messages.to_sentence
end
def destroy
@@ -94,8 +95,4 @@ class AccountsController < ApplicationController
def account_params
params.require(:account).permit(:name, :accountable_type, :balance, :start_date, :start_balance, :currency, :subtype, :is_active, :institution_id)
end
-
- def sync_account
- @account.sync_later
- end
end
diff --git a/app/views/accounts/new.html.erb b/app/views/accounts/new.html.erb
index 878cafc0..1e42a5d6 100644
--- a/app/views/accounts/new.html.erb
+++ b/app/views/accounts/new.html.erb
@@ -86,7 +86,7 @@
<%= label_tag :add_start_values, t(".optional_start_balance_message"), class: "pl-1 text-sm text-gray-500" %>
<div class="hidden peer-checked:flex items-center gap-2 mt-3 mb-6">
- <div class="w-1/2"><%= f.date_field :start_date, label: t(".start_date"), max: Date.current %></div>
+ <div class="w-1/2"><%= f.date_field :start_date, label: t(".start_date"), max: Date.yesterday %></div>
<div class="w-1/2"><%= f.number_field :start_balance, label: t(".start_balance") %></div>
</div>
</div>