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?
There doesnt seem to be a generic field for select boxes, but this worked for me:
rails generate administrate:field select
app/fields/select_field.rb
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
app/views/fields/select_field/_form.html.erb
<div class="field-unit__label">
<%= f.label field.attribute %>
</div>
<div class="field-unit__field">
<%= f.select(field.attribute, field.choices) %>
</div>
app/dashboards/user.rb
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
]

This 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).
app/dashboards/user.rb
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 choices.zip(choices) in the field class). It should be easy enough to taylor to your needs.
Hey, try using Field::Select :)
No, Field::Select just built a dropdown without options.
This was converting from the default-generated
role: Field::String.with_options(searchable: false),
to
role: Field::Select
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:
vendortype: Field::Select.with_options(
choices: User.vendortypes
),
rails is not picking up the choices.
If I try your code directly
choices = options.fetch(:choices)
choices.zip
I get a really crazy array, like this:
[
[0] [
[0] [
[0] "affiliate",
[1] 0
],
[1] [
[0] "affiliate",
[1] 0
]
],
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:
foo: Field::Select.with_options(collection: [:foo, :baz])
Please tell me how can I achieve something like this?
<%= select_tag :status, options_for_select([["disable", 0], ["enable", 1]]) %>
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 option_from_collection_for_select with options_for_select.
This implementation from https://github.com/thoughtbot/administrate/issues/533#issuecomment-235689237 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 require "administrate/fields/base" line, fields had to be field for me. Your solution works well for me though, so thank you!
Most helpful comment
@BillyParadise You can do:
foo: Field::Select.with_options(collection: [:foo, :baz])