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
Feature request/ Way to do dropdown/select boxes for enums...etc? #420
Comments
There doesnt seem to be a generic field for select boxes, but this worked for me: Run this:
Mimic these:
require "administrate/fields/base"
class SelectField < Administrate::Field::Base
def to_s
data
end
def choices
choices = options.fetch(:choices)
choices.zip(choices)
end
def selected_choice
data
end
end
<div class="field-unit__label">
<%= f.label field.attribute %>
</div>
<div class="field-unit__field">
<%= f.select(field.attribute, field.choices) %>
</div> Use in a Dashboard
ATTRIBUTE_TYPES = {
# All your other fields
role: SelectField.with_options(
choices: User.roles # Any array of choices. Add blank yourself if you need it.
)
}
FORM_ATTRIBUTES = [
# Other form fields
:role
] ProfitCaveatsThis implementation only works with a flat array of choices. If you are using ActiveRecord enums, be sure to get just the keys (or modify this implementation).
ATTRIBUTE_TYPES = {
some_enum: SelectField.with_options(
choices: User.some_enums.keys
)
} It also assumes that your form option texts are the same as your option values (hence the |
add custom field for select see: thoughtbot/administrate#420
Hey, try using Field::Select :) |
No, Field::Select just built a dropdown without options. This was converting from the default-generated Could you please be a bit more verbose in your suggestion? Thanks. |
@andrewhamon hope you're still following this project. I still can't for the life of me get your recommendation working. There have been some namespace changes since you proposed the above solution, so I am using:
rails is not picking up the choices. If I try your code directly
I get a really crazy array, like this:
This doesn't look right, which is likely why I'm not getting any results. Thoughts? Dropdowns on enums could be really useful if they worked :) |
@BillyParadise You can do:
|
Please tell me how can I achieve something like this? All I want is to render text "Enable", "Disable" and value "1", "0" for each option respectively. And then how can I show "Enable" for value "1" and "Disable" for value "0" on the index and show page? |
@ammarshah That's pretty bad it doesn't support that. I think you would have to create your custom Select. Create a question on Stackoverflow if you need help with that. Should just be a matter of swapping out |
This implementation from #533 (comment) worked for me out of the box. |
I'm going to close this one, as I'd like us to focus on just documenting this. #816 will consolidate these related issues. |
@andrewhamon For anyone else copying your pattern, there is a typo in the |
This works perfectly well. Thank you. |
Maybe its possible to do dropdowns though I was digging though the docs and I did not see a possible way to do them?
Is this a thing?
The text was updated successfully, but these errors were encountered: