Skip to content
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

Closed
ChrisCPO opened this issue Jan 19, 2016 · 11 comments
Closed

Feature request/ Way to do dropdown/select boxes for enums...etc? #420

ChrisCPO opened this issue Jan 19, 2016 · 11 comments

Comments

@ChrisCPO
Copy link

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?

@ChrisCPO ChrisCPO changed the title Feature request way to do dropdown select boxes for enums...etc? Feature request/ Way to do dropdown/select boxes for enums...etc? Jan 19, 2016
@andrewhamon
Copy link

There doesnt seem to be a generic field for select boxes, but this worked for me:

Run this:

rails generate administrate:field select

Mimic these:

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>

Use in a Dashboard

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
  ]

Profit

screen shot 2016-01-30 at 11 52 45 am

Caveats

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.

maripiyoko added a commit to maripiyoko/administrate-demo that referenced this issue Feb 28, 2016
@maxious
Copy link

maxious commented Jul 21, 2016

Hey, try using Field::Select :)

@BillyParadise
Copy link

BillyParadise commented Jul 28, 2016

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.

@BillyParadise
Copy link

BillyParadise commented Aug 23, 2016

@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 :)

@dan-klasson
Copy link

@BillyParadise You can do:

foo: Field::Select.with_options(collection: [:foo, :baz])

@ammarshah
Copy link

ammarshah commented Dec 8, 2016

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?

@dan-klasson
Copy link

@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.

@avk
Copy link

avk commented Jan 12, 2017

This implementation from #533 (comment) worked for me out of the box.

@nickcharlton
Copy link
Member

I'm going to close this one, as I'd like us to focus on just documenting this. #816 will consolidate these related issues.

@sgoldens
Copy link

@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!

@thubamamba
Copy link

@BillyParadise You can do:

foo: Field::Select.with_options(collection: [:foo, :baz])

This works perfectly well. Thank you.

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

No branches or pull requests