When using a custom field twice for the same Dashboard, I receive this error:
uninitialized constant Administrate::Field::EnumField
I am attempting to create a custom Field for an ActiveRecord enum property. Following the instructions in https://administrate-prototype.herokuapp.com/adding_custom_field_types, I create a new field. It is very basic. It only has the code provided by the generator. It works for a single column, but as soon as I attempt to use for an additional column, I get the above error.
e.g. This works
class CourseDashboard < Administrate::BaseDashboard
...
ATTRIBUTE_TYPES = {
id: Field::Number,
name: Field::String,
status: Field::EnumField,
...
This fails with uninitialized constant Administrate::Field::EnumField:
class CourseDashboard < Administrate::BaseDashboard
...
ATTRIBUTE_TYPES = {
id: Field::Number,
name: Field::String,
status: Field::EnumField,
semesters: Field::EnumField,
...
I reproduced this in a sample app at https://github.com/mattscilipoti/test_administrate_custom_enum. Just open app/dashboards/course_dashboard.rb and uncomment line#14 (semesters: Field::EnumField,).
@mattscilipoti I had the same until I tried to just access the class directly in AttributeTypes.
Try:
status: EnumField,
Instead of:
status: Field::EnumField,
Thx @joshuamcginnis, that workaround works for me. Not sure why the Field::EnumField becomes unavailable after it is has been used.
To clarify further…
You should access the class directly for each occurrence of your custom field. This caught me out for a few minutes.
I was using a custom field in two different dashboards. In one I had Field::EmailField and in the other i had EmailField. This continued to error. In this case I needed to use EmailField in bother instances.
Hmm to me it looks like the main issue here is that EnumField is not actually under the Field namespace.
In your sample app you have class EnumField with no surrounding module namespace. To be able to call Field::EnumField, I'd expect it to look more like this
module Field
class EnumField < class EnumField < Administrate::Field::Base
end
end
When you create a custom field, it's just under the global namespace of your app, so you reference directly as EnumField.
Hope that helps!
I'm not sure why, but I too had to use the workaround of including just the class name.
When I tried the suggestion of @carlosramireziii, I got a load error "Unable to autoload constant MyField, expected [correct full path to file setup exactly as Carlos suggested] to define it".
Most helpful comment
@mattscilipoti I had the same until I tried to just access the class directly in
AttributeTypes.Try:
Instead of: