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

Enum fields #533

Closed
hauleth opened this issue Apr 4, 2016 · 8 comments
Closed

Enum fields #533

hauleth opened this issue Apr 4, 2016 · 8 comments

Comments

@hauleth
Copy link

hauleth commented Apr 4, 2016

It would be really useful to have some way to entry values for has_entry.

@stevenpetryk
Copy link

stevenpetryk commented Jul 27, 2016

rails g administrate:field enum

enum_field.rb

require "administrate/field/base"

class EnumField < Administrate::Field::Base
  def to_s
    data
  end

  def select_field_values(form_builder)
    form_builder.object.class.public_send(attribute.to_s.pluralize).keys.map do |v|
      [v.titleize, v]
    end
  end
end

_form.html.erb

<div class="field-unit__label">
  <%= f.label field.attribute %>
</div>
<div class="field-unit__field">
  <%= f.select field.attribute, field.select_field_values(f) %>
</div>

(_show/_index).html.erb

<%= field.to_s.titleize %>

@carlosramireziii
Copy link
Collaborator

@hauleth Does the custom field that @stevenpetryk illustrated above work for your needs? If so, would you or he like to take a stab at creating it as a plugin?

Here's an example:
https://github.com/thoughtbot/administrate-field-image

@brndn4
Copy link

brndn4 commented Mar 24, 2017

Worked for me. You might want to change EnumField#to_s to

def to_s
  data.to_s
end

In case the enum data is nil

@carsonbland
Copy link

@stevenpetryk what is the benefit over Field::Select.with_options(collection: Model::ENUM) ?

@stevenpetryk
Copy link

@carsonbland I'm not sure – it may very well be the case that that was undocumented/impossible back when I posted that response. If that works, then go for it!

@BenMorganIO
Copy link
Collaborator

Closing this, since a custom enum field with bin/rails g administrate:field enum solves the issue.

@fercreek
Copy link

@stevenpetryk great solution! Thanks!!

@joallard
Copy link

@BenMorganIO Sounds like this should be part of core

saramic added a commit to SelenaSmall/search-term that referenced this issue Jan 6, 2019
attempted to use an Enum field in administrate with no success so ended
up using string
  `rails g administrate:field enum`

as enum did nothing much and did not create a select box which would
have been helpfull as per example in
  thoughtbot/administrate#533 (comment)
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

8 participants