This blog is part of our Rails 5 series.
we want a single form to cater to multiple models.
By using this we can easily
provide attributes for associated models.
In Rails 4.x, if a validation fails for one or more of the associated models, then it is not possible to figure out from error message, which of the associated model object is the error related to.
In the example above we can see that if this error message is sent as JSON API, we cannot find out which variant save failed because of which attribute.
This works well when we render forms using Active Record models, as errors are available on individual instances. But, the issue arises with an API call, where we don’t have access to these instances.
Rails 5 allows indexing of errors on nested attributes
In Rails 5, we can add an index to errors on nested models.
We can add the option
index_errors: true to
association to enable this behavior on individual association.
Using global configuration
In order to make this change global,
we can set configuration
config.active_record.index_nested_attribute_errors = true
false by default.
This will work exactly same as an example with
has_many :variants, index_errors: true in