Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I display a translation of a database column name as a table heading in a view, Rails 3

I have a model called User with an attribute called current_sign_in_at. In my en.yml file I have the display name as such…

en-GB:
  activerecord:
    attributes:
      user:
        current_sign_in_at: "Last sign-in"

…which allows me to display the desired form label ("Last sign-in") using = f.label :current_sign_in_at.

But how can I use this same translation for a table header, i.e. not in a form?

%th= :current_sign_in_at
like image 220
matharden Avatar asked Oct 03 '12 16:10

matharden


1 Answers

You can use the static method "human_attribute_name", See the doc here on API dock

In your case:

%th= User.human_attribute_name :current_sign_in_at

(use User.human_name to display the model name translated with I18n in en.activerecord.models.user)

Hope this helps!

like image 91
MrYoshiji Avatar answered Sep 28 '22 00:09

MrYoshiji