1

The only way I can get my Simple form to display an error is via

  <% errors = f.object.errors.messages.collect { |k,v| "#{k}: #{v}" if v.length > 0 }.join("\n")%>
  <%= f.error_notification message: "Errors: #{errors}" %>

Which seems way too complicated.

Scenario:

User table and nested, polymorphic table of social_profile links (facebook, twitter, etc).

If the user does not fill out any of the nested form when creating a new account, the profile inputs/params are empty strings and it saves fine.

However, if the user deletes all the profile links' input fields then the association to the social_profile table will not be created. Thus, if the user later tries to edit his account the nested social_profile form will not be displayed.

Adding validates :social_profile, presence: true, associated: true to the User's model prevents this error from happening, but there is no indication of what occurred given to the user. The best I could come up with is the original code, but it seems like there should be a better way.

0

1 Answer 1

2

@Brad Werth linked me to this SO post which helped me construct the following answer:

<%= f.object.errors.full_messages.join(", ") if f.object.errors.any? %>

Grabs the error'ed object from the form, gets the "full_messages" (ie "Description cannot be blank") and joins them together with commas.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.