-->

i18n for select boxes

2019-02-13 06:29发布

问题:

I have a model named Role. And i am using the helper below in a form. Is there a way to change the value of name attribute to another language?

<%= f.collection_select :role_id, Role.all, :id, name, {} -%>

locales/de.yml

de:
  role:
   admin: "something"
   editor: "something something"

回答1:

In the model:

class Role < ActiveRecord::Base
  def translated_name
    I18n.t(name, :scope => 'role')
  end
end

In the view:

<%= f.collection_select :role_id, Role.all, :id, :translated_name -%>