Rails 5: i18n for listing a model with names

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.

0
enums ruby ruby-on-rails
Feb 12 '17 at 7:41
source share

No one has answered this question yet.

See similar questions:

56
How to use i18n with Rails 4 listings
9
Problem using i18n gem with partial template files

or similar:

3575
How to list an enumeration?
2964
How to cast int to enum?
1954
What is the preferred syntax for defining enums in JavaScript?
1818
How to get enum value from string value in Java?
1658
Get int value from enum in C #
1555
Comparing Java enumeration elements: == or equals ()?
1204
What is the meaning of the Enlue [Flags] Enum attribute in C #?
1146
How can I introduce "Enum" in Python?
1077
What is typedef enumeration in Objective-C?
1012
Can you see all the enumeration values?



All Articles