Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I tell Rails how to correctly capitalize an acronym?

I have a field called sui in one of my models. It stands for "Standard User Identifier." When there are validation errors on the field, Rails prints "Sui is required" or "Sui is already taken."

How can I tell Rails that 'sui'.titleize is "SUI"? I looked at Inflector.human, but that isn't quite right.

like image 334
James A. Rosen Avatar asked Apr 29 '09 16:04

James A. Rosen


1 Answers

In such cases I use custom_err_msg plugin. As it is installed you can give custom error messages like this:

validates_presence_of :sui, :message => '^SUI is required'

When you put ^ at the begining then Rails don't put field name.

EDIT: There is another plugin i18n_label used for translations, but with it you can in very simple way replace a name of your field with something nicer (on plugin page there is an example). It will substitute name in:

<%= f.label :sui %>
YourModel.human_attribute_name "sui"

and in error messages.

like image 64
klew Avatar answered Sep 28 '22 07:09

klew