I am a bit stuck with i18n for listing in a model with names. At the moment, I have a line in the /app/media/medias.rb model as follows:
enum territory: { local: 1, global: 2 }
in the modal window / views / media / medias / _newmedia.html.erb I have this:
<%= f.select :territory, Media::Medias.territories.keys.collect { |g| [(t'.territories.#{g.downcase}'), g] }, {}, {class:"form-control mb"} %>
and in /config/locales/models/medias.en.yml I have this:
en: media: medias: territories: local: Local_EN global: Global_EN
In the console, I see that the request is completed, the error does not occur, but my view does not appear. When I go to the simple Media::Medias.territories.keys , I get a drop-down menu with key values ββin my View. How can I fix this, please? Thank!
So far, I have been trying to implement the code from this answer . In view of this, the translation gives beautifully, but when I created the action, I got an error, because it tried to save not the enumeration key, but the localized value.
Decision
Well, this answer seems to be the solution in my case. If someone needs here, then how it should be done with the model with names in my example:
1) in the model /app/media/medias.rb
enum territory: { local: 1, global: 2 } def self.territory_attributes_for_select territories.map do |territory, _| [I18n.t("activerecord.attributes.#{model_name.i18n_key}.territories.#{territory}"), territory] end end
2), then taking into account partial / views / media / medias / _newmedia.html.erb
<%= f.select :territory, options_for_select(Media::Medias.territory_attributes_for_select), {}, {class:"form-control mb"} %>
3) and finally in /config/locales/en.yml
en: activerecord: attributes: media/medias: #Important! This is how you define namespaced model! territories: local: "Local EN" global: "Global EN"
I can confirm that the correct locale is shown in the view, and then the values ββare stored in the create action.