Reputation: 3905
Is it possible to set a display name for a database field, instead of using the auto-generated one?
I've got a field bbe_date
, and in screens, I am using 'Best Before' as the displayed string. I have been through a few views setting this manually, but is there a better way?
This would hopefully take effect wherever the field name is sent to the browser in human-readable form. I'm particularly thinking about validation errors as well (since that's the bit I haven't already handled manually!) - my validation code is doing:
record.errors.add :bbe_date, 'not valid'
but unless I specially intercept this, I just see 'Bbe date not valid' as the validation error.
Upvotes: 6
Views: 2608
Reputation: 37517
One way do do this is to override the behavior of human_attribute_name
which gives you the default display name. In your model:
HUMANIZED_ATTRIBUTES = {
:bbe_date => "Best Before"
}
def self.human_attribute_name(attr, options={})
HUMANIZED_ATTRIBUTES[attr.to_sym] || super
end
Note that if this is Rails 2, you'll need to remove the options param from human_attribute_name.
Upvotes: 0
Reputation: 46677
You need internationalization (I18N). Yup, even if you just want English.
Modify config/locales/en.yml, and set:
en:
activerecord:
attributes:
your_lower_case_model_name:
bbe_date: Best Before
Upvotes: 13