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;
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 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:
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 thisI 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 %>