Administrate: Enum attribute type

Created on 5 Nov 2015  路  12Comments  路  Source: thoughtbot/administrate

it's seems like i'm unable to set enum field type

Most helpful comment

If you guys are still having trouble with the Enum type I made it a gem, feedback is appreciated 馃槉. https://rubygems.org/gems/administrate-field-enum

All 12 comments

You can generate field type for enum:

$ rails generate administrate:field enum
  create  app/fields/enum_field.rb
  create  app/views/fields/enum_field/_show.html.erb
  create  app/views/fields/enum_field/_index.html.erb
  create  app/views/fields/enum_field/_form.html.erb

For enum would only need to modify the file form.html.erb:

<%= f.label field.attribute %>
<%= f.collection_select(
        field.attribute,
        f.object.class.public_send(field.attribute.to_s.pluralize),
        :first,
        :first,
        {},
        { multiple: false },
    )
%>

Then open the file dashboard and replace the Field::Number onEnumField. Everything should work.

@mgrachev thanks! Looks like I missed this issue.

@davit-khaburdzania - if that works for you, feel free to close the issue!

@mgrachev checked it and everything seems to be working. Thanks a lot

:tada:

@graysonwright What do you think about adding Field::Enum into built-in field list ? :smiley:

@pkrefta I like the idea, added to the to-do list.

Enum field with humanized text

_enum_field_:

require 'administrate/fields/base'

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

__form.html.erb:_

<%= f.label field.attribute %>
<%= f.select(
  field.attribute,
  options_for_select(
    f.object.class.public_send(field.attribute.to_s.pluralize)
      .map { |k, v| [k.humanize, k] },
      f.object.public_send(field.attribute.to_s)),
  include_blank: false) %>

This breaks with current version (0.1.3) - no administrate / fields / base

@BillyParadise - sorry about that. There were breaking changes in the most recent version. Here are the release notes: https://github.com/thoughtbot/administrate/releases/tag/v0.1.4

In this case, here's the fix:

# Change any instances of this...
require "administrate/fields/base"
# ...to this:
require "administrate/field/base"

appreciated!

If you guys are still having trouble with the Enum type I made it a gem, feedback is appreciated 馃槉. https://rubygems.org/gems/administrate-field-enum

Thanks Balbina,It worked perfectly for me!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gracewashere picture gracewashere  路  3Comments

Reedian picture Reedian  路  4Comments

ghost picture ghost  路  4Comments

MatthiasRMS picture MatthiasRMS  路  3Comments

migu0 picture migu0  路  3Comments