-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
The state of enums in simple_form #1668
Comments
Here's what I'm using for enum in case it helps someone else - it seems to make everything work. If code is not interested in being changed, we can at least add this to the readme to make utilizing enums easy? model/user.rb:
view/users/_form.rb:
|
Rails 7.1 now allows you to validate enum columns, which allows a bit more fine grained control of when to show blank options - https://api.rubyonrails.org/classes/ActiveRecord/Enum.html. We've wrapped the
And rails initializer to register the type:
|
Thanks @tvongaza! If anyone else wants to use this, you may need to change the class definition to this: class EnumInput < SimpleForm::Inputs::CollectionSelectInput I replaced the collection function with this: def collection
@collection ||= begin
raise ArgumentError,
"Collections are inferred when using the enum input, custom collections are not allowed." if options.key?(:collection)
object.defined_enums[attribute_name.to_s].keys.map do |key|
[key.to_s.capitalize, key]
end
end
end |
Hi all,
Out of curiosity, over the years, I've aways had to type extra code to let the enums work in simple form. Why is this still the case? Basically any other fields just works out of the box. Is there here a reason I am totally missing?
When I think simple form I think simplicity, no need to worry, and no typing too much. It just works for all other types except enums. Any reason why?
The text was updated successfully, but these errors were encountered: