Skip to content

Enums issue with the form - #160

Open
dixpac wants to merge 3 commits into
josefarias:mainfrom
dixpac:dix/enum_issue
Open

Enums issue with the form#160
dixpac wants to merge 3 commits into
josefarias:mainfrom
dixpac:dix/enum_issue

Conversation

@dixpac

@dixpac dixpac commented May 4, 2024

Copy link
Copy Markdown
Contributor

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
Copy Markdown
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