Issue adding a base error to a nested object using Active Admin
I've got the following issue:
I am using Active Admin 1.2.1 and I am trying to add a base error to a nested attribute. While I can see it being added before the form is rendered when I inspect the error object in the view the base error's array is empty.
I have the following associations:
class Client < ApplicationRecord has_one :card accepts_nested_attributes_for :card end class Card < ApplicationRecord belongs_to :client end
And the following (pseudocode) in app/admin/clients.rb
ActiveAdmin.register Client do ... controller do def create call_third_party_to_validate_card super if issues_validating_card resource.card.errors.add(:base, errors_from_third_party) end end end end
If I put a debugger statement after I explicitly add errors I can see that they exist. However when I inspect a card's errors in the view it is an empty array. This includes using both
card is the block variable for the nested form) Similarly if I add an error to a specific card attribute I cannot see it in the view.
My questions are why is this happening and how can I display the errors in the form?
After more exploration it looks like my issue is that the page is rendered before I'm adding errors to the object. That explains why they didn't show up in the view.
As such I was able to fix my issue by not calling super and instead using a version of the code in
InheritedResources::Action#create that fits my needs.
Which I guess leads me to asking if there is a better way to approach my problem above? That is: adding errors to an object before the page is rendered but after creating the object using