Since administrate does not yet have support for multiple selects. Has anyone created a custom multiple select? Preferably one that works like the HasMany
select.
Asked
Active
Viewed 1,633 times
3

dan-klasson
- 13,734
- 14
- 63
- 101
1 Answers
6
Here it is in case someone else needs it:
app/fields/multiple_select_field.rb
# app/fields/multiple_select_field.rb
require "administrate/field/base"
class MultipleSelectField < Administrate::Field::Select
def to_s
data
end
def self.permitted_attribute(attribute)
{ attribute.to_sym => [] }
end
def permitted_attribute
self.class.permitted_attribute(attribute)
end
end
app/views/fields/multiple_select_field/_form.html.erb
# app/views/fields/multiple_select_field/_form.html.erb
<div class="field-unit__label">
<%= f.label field.attribute %>
</div>
<div class="field-unit__field">
<%= f.select(
field.attribute,
options_from_collection_for_select(
field.selectable_options,
:to_s,
:to_s,
field.data.presence,
),
{}, multiple: true,
) %>
</div>
app/views/fields/multiple_select_field/_index.html.erb
# app/views/fields/multiple_select_field/_index.html.erb
<%= field.to_param.join(', ') %>
app/views/fields/multiple_select_field/_show.html.erb
# app/views/fields/multiple_select_field/_show.html.erb
<%= field.to_param.join(', ') %>

Community
- 1
- 1

dan-klasson
- 13,734
- 14
- 63
- 101
-
Good stuff! How exactly do I incorporate this in attribute_dashboard.rb? – Esseme May 15 '19 at 10:23
-
@Esseme Haven't worked with this in years but assuming you just have to use it in your view like you would with any other administrate field. – dan-klasson May 15 '19 at 11:52
-
hi @dan-klasson, I am trying to implement bulk delete by providing checkbox to select records and delete multiple at once, can you please point me in the right direction as to how i can achieve this, thanks. – opensource-developer Oct 17 '19 at 12:40
-
1with probably the latest version of administrate gem, you would need to update `def self.permitted_attribute(attribute)` to `def self.permitted_attribute(attribute, _options = nil)` – opensource-developer May 09 '22 at 11:41