-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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 - Select tag dont keep saved option selected on edit page #1939
Comments
Hm, this might be a bug, but I'm not sure. I wonder if it's something that we specifically do not support, for some reason. I noticed that there's a plugin to support enums: https://github.com/Valiot/administrate-field-enum, however I haven't tried it, so I don't know if it would solve your problem. Perhaps this should be supported by Administrate? I'm not sure. It might be tricky for some reason. If you want to give it a try and provide a PR, we could have a look at it. |
There's nothing special about an enum from the perspective of a select element. The enum values are simply the options available to choose from. The thing is enums are usually stored in the database as integers, so @DavidGeismarLtd's enum definition appears to be atypical and either incorrect or possibly using a gem that allows string-based enums. I'd expect to see this instead:
I didn't try to replicate this, so just taking a guess based on normal enum usage. If a value is actually getting saved (which can't be seen here so is not a certainty), it's probably being saved as 0 1 or 2 in the database. Any saved data associated with this field would not match the values being passed in, hence nothing will ever show up preselected. |
I was able to reproduce this today. The issue stems from how ActiveRecord works with enums, which can be a bit too clever depending on your taste. So far, our However if you use enums you get an abstraction layer such that your code never has to see the low-level values. Instead Fortunately there is a way to detect when a field is actually an enum, so I'm hoping I can use this in order to do the right thing in the enum case. I have put something together that seems to work: #2348 |
A fix for this is ready for review at #2348 |
Fixes #1939 In the spirit of "make the change easy, then make the easy change", this PR includes a refactor of `Field::Select`, moving the logic out of the template and into the field class, where the actual functionality (and any future one) can be handled and tested more easily. I have also written more exhaustive specs for the behaviour of this field, which helped me not breaking existing functionality while refactoring (or so I hope!).
create a select form an enum field.
In my model Program :
enum publication_status: { 'publié' => 'published', 'a venir' => 'upcoming', 'brouillon' => 'draft' }, _prefix: :publication_status
Keys are the "human" readable values, values are the value to be stored in db
In my administrate dashboard :
This generates the following html :
However when I then update the resource and go back on the edit page the saved value is not the option selected, instead it is always the first one.
Am I not following the right approach for using enums with administrate or is this a bug ?
The text was updated successfully, but these errors were encountered: