2

Nested forms are great, but I have noticed that it can sometimes lead to error message that are oddly formatted.

To give a concrete example: I have a form that lets someone create a new Account. Each account has one or more Users (has_many). The signup form uses the Account model for creating the form and also includes a number of fields for creating the first User (as an account must have at least one user). In other words, it is a nested form.

Because an account can have many users, the prefix of the error messages says "users" instead of "user". Also, the error messages use the relationship name (users) and the attribute name (for example, "password") to construct the error message. This results in error message such as "Users email can't be blank." instead of "Email can't be blank.".

Is there a way to customize the error message or omit "users" from the error message?

Bart Jacobs
  • 9,022
  • 7
  • 47
  • 88

1 Answers1

2

In your :message parameter of your validation, you can add a caret which will strip the default message.

   :message => "^ Email can't be blank"
Kyle C
  • 4,077
  • 2
  • 31
  • 34
  • Thanks for your answer, Kyle. This looks like a good solution, but is there a way to get the same effect without providing a message for each validation as I would like to avoid that? – Bart Jacobs Jul 10 '12 at 17:33
  • You could look into overwriting the activerecord yml , http://stackoverflow.com/questions/808547/fully-custom-validation-error-message-with-rails http://stackoverflow.com/questions/3808361/where-are-default-validation-error-messages-in-rails-3-0 – Kyle C Jul 10 '12 at 18:01
  • 8
    the caret doesn't seem to work anymore... Has it been replaced by something else? – Matt Wonlaw Feb 20 '13 at 03:38