Skip to content
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

Enums issue with the form #160

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Conversation

dixpac
Copy link
Contributor

@dixpac dixpac commented May 4, 2024

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.ratings

This style will work only if enum key and value are same (string) for example:

class Movie < ApplicationRecord
   enum rating: %w[ G PG PG-13 R NC-17 ].index_by(&:itself)
end

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 rating

I'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 #keys on the enum to pass the enum keys,
like this

<%= form.combobox :rating, Movie.ratings.keys %>

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 %>

@dixpac dixpac marked this pull request as draft May 4, 2024 13:21
@dixpac dixpac marked this pull request as ready for review May 4, 2024 13:21
@josefarias
Copy link
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants