3

由于 administrate 尚不支持多选。有没有人创建自定义多选?最好是一个像HasMany选择一样工作的。

4

1 回答 1

5

这是以防其他人需要它:

应用程序/字段/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

应用程序/视图/字段/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>

应用程序/视图/字段/multiple_select_field/_index.html.erb

# app/views/fields/multiple_select_field/_index.html.erb
<%= field.to_param.join(', ') %>

应用程序/视图/字段/multiple_select_field/_show.html.erb

# app/views/fields/multiple_select_field/_show.html.erb
<%= field.to_param.join(', ') %>
于 2016-11-20T07:06:25.450 回答