Enums issue with the form - #160
Open
dixpac wants to merge 3 commits into
Open
Conversation
dixpac
marked this pull request as draft
May 4, 2024 13:21
dixpac
marked this pull request as ready for review
May 4, 2024 13:21
Owner
|
Thanks @dixpac! You're right, the enum capabilities need to be better documented. Let me do some thinking on this. I've been planning on spending a good while on docs soon, and this needs to be part of it. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR is opened just to describe the issue with enums and forms;
If we follow official documentation or repos showcase page we have
form.combobox :rating, Movie.ratingsThis style will work only if enum key and value are same (string) for example:
If we use default rails enum approach where enum value is stored as an integer inside the db column, this will not work, Rails will fail to set the enum value and raise an exception:
ArgumentError: '3' is not a valid ratingI've added te system test just to confirm this issue.
In order to make this work for default enum approach we would need to use
#keyson the enum to pass the enum keys,like this
I don't think this is engines fault, but maybe(just maybe) this needs to be documented, or at least we should change examples in the showcase page to show the valid state;
This
<%= form.combobox :rating, Movie.ratings.keys %>instead of this
<%= form.combobox :rating, Movie.ratings %>