Two models, Organization and User, have a 1:many relationship. I have a combined signup form where an organization plus a user for that organization get signed up.
The p
This is very related to this question. The key is that <%= render 'shared/error_messages', object: f.object %>
is, I assume, only calling the .errors
method on the object it is passed (in this case, organization
).
However, because the user
errors reside with the user
object, they won't be returned and therefore will not be displayed. This requires simply changing the view logic to also display the results of .errors
on the various user
models. How you want to do so is up to you. In the linked thread, the accepted answer had the error message display code inline instead of in a partial, so you could do it that way, but it would be somewhat redundant.
I would modify my shared/error_messages.html.erb
file to check for another passed local called something like nested_models
. Then it would use that to search the associated models and include the errors on that. We just would need to check whether it is defined first so that your other views that don't have a nested model won't cause it to raise an error.
shared/error_messages.html.erb
<% if object.errors.any? %>
<% end %>
Then you would just need to change a single line in your view:
<%= render partial: 'shared/error_messages', locals: { object: @organization, nested_models: @organization.users } %>