How can I display a translation of a database column name as a table heading in a view, Rails 3
-
13-12-2019 - |
Question
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
Solution
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!
OTHER TIPS
Another option for internationalized table headings is the table_for gem.
This will let you write erb code like:
<%= table_for @user do |table| %>
<% table.column :current_sign_in_at %>
<% end %>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow