Hi I would like to understand how are the error messages displayed in ruby on rails, more importantly how is the order determined in which the error messages are displayed on the page.
Thanks for your time.
Hi I would like to understand how are the error messages displayed in ruby on rails, more importantly how is the order determined in which the error messages are displayed on the page.
Thanks for your time.
Active Record offers many pre-defined validation helpers. Every time a validation fails, an error message is added to the object’s errors collection, and this message is associated with the field being validated. See this excellent guide for more details on validation.
I believe that since the validation targets are stored in a hash, their order is never guaranteed. But there are many existing plugins to customize and enhanced the default validation. A quick search yields CleverValidation and CustomErrorMessage...
You might be interested in this blog post as well, which talks on how to customize the validation yourself.
Most common way to display errors is to use helper error_messages
:
<%= f.error_messages %>
As far as I know it adds some html tags to error messages that are stored in @my_object.errors
hash. Error messages are ordered hash and they are in order that your validations are specified in model (at least it works like this in my applications). You can read more about Errors class here.
Since error_messages
are just a helper, you can write your own helper to display error messages.