Administrate: fields: Checkbox field with >2 options?

Created on 5 Jan 2020  路  4Comments  路  Source: thoughtbot/administrate

I have multiple checkboxes for one of my fields. I'm trying to reproduce this in the edit view of the dashboard but haven't figured it out after reading through the docs.

The form view I was trying to reproduce shows a series of checkboxes and the user can select multiple options. The selected options are stored as an array.

I'm new to administrate and not a very experienced programmer. I assume there's something I'm missing? Also checked Stackoverflow and didn't find anything.

Most helpful comment

hi @conwayanderson ,

In order to make the checkbox fields accessible in your edit form, you will have to create a Custom Field type for your checkboxes. in this case, mine is employment type.

Screen Shot 2020-01-07 at 1 17 27 AM

step 1) run rails generate administrate:field employment_type
that will create these files: app/fields/employment_types_field.rb
app/views/fields/employment_types_field/_show.html.erb
app/views/fields/employment_types_field/_index.html.erb
app/views/fields/employment_types_field_field/_form.html.erb

step 2) edit the custom field class in the newly generated folder, fields/employment_types_field like this:
Screen Shot 2020-01-07 at 1 02 13 AM
(I opted to create a db table + model for holding Employment Categories and return all categories)

step 3) go to app/views/fields/employment_types_field_field/_form.html.erb
and create the checkboxes using the field.employment_types in this form partial.
(field is an object of type(EmploymentTypesField))
Screen Shot 2020-01-07 at 1 08 49 AM

step 4) next include the custom field type in the dashboard file you want to show the field at.
For me, I wanted to show it in customer_dashboard.rb. You will have to add the field type in 2
places, in the ATTRIBUTE_TYPES constant hash and then also in the FORM_ATTRIBUTES array.
Screen Shot 2020-01-07 at 1 13 21 AM

step 5) Last step you will have to go to the admin controller for the dashboard whose form you just modified. you will have to override the Update action in the controller. In this case, you will simply need to modify the strong params being passed to the update action to account for the checkbox input.
Screen Shot 2020-01-07 at 1 16 49 AM

that should be it.

All 4 comments

Hello @conwayanderson. Would you be able to create a question for this on StackOverflow, providing the following details?

  • Type of the field. For example, the migration introducing it, or the relevant table description from the config/schema.rb file.
  • A screenshot of the field (if shown anywhere else in your application), or a mockup of what you expect to see.

(Asking for StackOverflow because it's a more appropriate place for this question, and other people may benefit better from the answer there).

Leave a link to the question here and I'll have a look.

Thank you!

hi @conwayanderson ,

In order to make the checkbox fields accessible in your edit form, you will have to create a Custom Field type for your checkboxes. in this case, mine is employment type.

Screen Shot 2020-01-07 at 1 17 27 AM

step 1) run rails generate administrate:field employment_type
that will create these files: app/fields/employment_types_field.rb
app/views/fields/employment_types_field/_show.html.erb
app/views/fields/employment_types_field/_index.html.erb
app/views/fields/employment_types_field_field/_form.html.erb

step 2) edit the custom field class in the newly generated folder, fields/employment_types_field like this:
Screen Shot 2020-01-07 at 1 02 13 AM
(I opted to create a db table + model for holding Employment Categories and return all categories)

step 3) go to app/views/fields/employment_types_field_field/_form.html.erb
and create the checkboxes using the field.employment_types in this form partial.
(field is an object of type(EmploymentTypesField))
Screen Shot 2020-01-07 at 1 08 49 AM

step 4) next include the custom field type in the dashboard file you want to show the field at.
For me, I wanted to show it in customer_dashboard.rb. You will have to add the field type in 2
places, in the ATTRIBUTE_TYPES constant hash and then also in the FORM_ATTRIBUTES array.
Screen Shot 2020-01-07 at 1 13 21 AM

step 5) Last step you will have to go to the admin controller for the dashboard whose form you just modified. you will have to override the Update action in the controller. In this case, you will simply need to modify the strong params being passed to the update action to account for the checkbox input.
Screen Shot 2020-01-07 at 1 16 49 AM

that should be it.

That's two answers now 馃槃 (one here and another one on Stack Overflow). I hope that helps!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

preist picture preist  路  4Comments

ghost picture ghost  路  4Comments

ACPK picture ACPK  路  4Comments

drewtunney picture drewtunney  路  3Comments

trandoanhung1991 picture trandoanhung1991  路  3Comments