Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a display name for a field in Rails

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.

like image 769
asc99c Avatar asked Dec 16 '11 13:12

asc99c


1 Answers

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
like image 102
Chowlett Avatar answered Oct 18 '22 22:10

Chowlett