Is automatic I18n broken for enum fields?
I'm not sure whether this is a bug or not, so I first posted on Stack Overflow:
http://stackoverflow.com/questions/23567791/rails-4-1-enums-and-simple-form-how-to-translate-them
Sadly nobody seems to know an answer. Please take a look at it.
Thank you.
It should work as any array collection but the values need to be symbols to it work. I'm sure enum returns strings and we don't support i18n with string and we don't plan to support since it will add i18n overhead for all the string collections.
I'm not sure whether I get this.
The following is translated:
= f.input :sex, as: :radio_buttons, collection: [:male, :female]
While the following is not:
= f.input :sex, as: :radio_buttons, collection: {male: 0, female: 1}
Because MyModel.sexes returns {male: 0, female: 1}, I think SimpleForm should translate it the same way as it does with [male: 0, :female].
And if not: how should I handle enums in forms?
Simple Form translate [:male, :female] because it is an array of only symbols, { male: 0, female: 1 } is not an array, Simple Form doesn't know how to handle it correctly and you will have to help it.
And if not: how should I handle enums in forms?
I don't know. Never tried to do it. Maybe calling to_a on the return of the enum definition would help to have the correct input but since the value of the collection array is not symbol Simple Form can't translate.
Since Simple Form can't automatically do this you will have to do it yourself calling translate with the enum keys.
Wouldn't it make sense to make SimpleForm smart enough to recognise hashes in the form of enums? I mean: enums are a new Rails feature and will be used more and more often in the future. It feels unnecessary that I should translate every single enum field myself, only because SimpleForm (which does soooooo much useful stuff already) doesn't recognise it.
If you don't want to guess too much about what's in a collection, how about introducing an enum: parameter (instead of collection: f.object.class.sexes)?
= f.input :sex, as: :radio_buttons, enum: :sexes
I really don't want to add more logic to Simple Form to support enum right now. The reason is, we (the Rails team) don't recommend to use enums are user facing features (this means on forms, or with user input). Right now, enum feature only support very well internal usage, there are some problems of using it with user input.
Maybe when enum is properly supported to accept user input we can implement this here.
@jmuheim you might want to use enumerize gem for enums. It supports SimpleForm out of the box though. :)
Right now, enum feature only support very well internal usage, there are some problems of using it with user input.
This is very interesting information. Thank you.
you might want to use enumerize gem for enums. It supports SimpleForm out of the box though. :)
Perfect! I will check it out.
Maybe somebody will like this small workaround to this issue.
Hi,
I there a "standard" way to do this now?
Personnaly I use an other workaround to use simple_form tree in yaml:
In the view :
<%= f.input :category_eq, as: :select, :collection => EdgeFacility.categories, :label_method => lambda { |category| t("simple_form.options.edge_facility.category.#{category[0]}") } %>
In yaml :
simple_form:
options:
edge_facility:
category:
simple: "Simple"
travelator: "Tapis roulant"
door: "Porte"
lift: "Ascenseur"
mecanical_stairs: "Escalier mécanique"
stairs: "Escaliers"
In the model
class EdgeFacility < ActiveRecord::Base
has_many :edges
enum category: [ :simple, :travelator, :door, :lift, :mecanical_stairs, :stairs ]
enum door_type: [ :sas, :portillon, :tripode, :portes_battantes ]
end
@ldonnet I'm using the https://github.com/brainspec/enumerize gem now and it's working really nice.
Keep in mind:
Right now, enum feature only support very well internal usage, there are some problems of using it with user input.
Another useful gem is https://github.com/zmbacker/enum_help .
Enum_help can help ActiveRecord::Enum feature to work fine with I18n and simple_form.
Thanks @jmuheim and @zmbacker for your help. I'm using now enumerize gem and it perfectly fits my needs. Enum feature like you said is more for internal features.
I found my issue here, so I thought I'd share my solution: http://approaching236.github.io/2015/10/15/using-an-activerecord-enum-with-simpleform.html
I follow @approaching236 's guide.
And it should be something in simple_form.zh-TW.yml too, e.g.:
zh-TW:
simple_form:
+ options:
+ department:
+ division:
+ day: "日間部"
+ night: "夜間部"
+ system:
+ bachelor: "學士"
+ master: "碩士"
+ phd: "博士"
Three years ago, @rafaelfranca wrote:
I really don't want to add more logic to Simple Form to support enum right now. The reason is, we (the Rails team) don't recommend to use enums are user facing features (this means on forms, or with user input). Right now, enum feature only support very well internal usage, there are some problems of using it with user input.
Is this still the Rails team advice? I have enums in my models, they seem to work well for behavioural switches, they're easier to work with in Rails 5 since we have _prefix: true now. I don't recall reading anything in the guides or API doco saying "enums are bad for user data".
In the meantime I'm just doing
= f.input :display_mode, as: :select, collection: Post.display_modes.keys.map(&:to_sym), include_blank: false
and it seems fine...
I know that it is old, but you can also use locales to translate enums, as:
# user.rb
enum gender: { not_defined: 0, male: 1, female: 2 }
#view.html.slim
= f.input :gender, required: true
#enums.pt-BR.yml
pt-BR:
enums:
user:
gender:
not_defined: Não definido
male: Masculino
female: Feminino
Hope that it helps someone.
Most helpful comment
Hi,
I there a "standard" way to do this now?
Personnaly I use an other workaround to use simple_form tree in yaml:
In the view :
In yaml :
In the model