管理员

时间:2016-11-19 12:31:34

标签: ruby-on-rails ruby rails-administrate

由于管理员尚未支持多项选择。有人创建了自定义多重选择吗?最好像HasMany选择一样工作。

1 个答案:

答案 0 :(得分:4)

以防其他人需要它:

应用程序/场/ 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(', ') %>