管理中的多个 select
Multiple select in administrate
因为管理尚不支持多个 select。有人创建了自定义倍数 select 吗?最好是像 HasMany
select.
这样工作的
如果其他人需要它:
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(', ') %>
因为管理尚不支持多个 select。有人创建了自定义倍数 select 吗?最好是像 HasMany
select.
如果其他人需要它:
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(', ') %>