如何在活动管理中的多个select中填充值(使用select2)

时间:2017-01-20 04:07:00

标签: ruby-on-rails activeadmin select2

我在我的有效管理表单块中有以下内容。不幸的是,即使手动将它们加载(如下所示),我也无法加载选择:

f.input :hearing_loss_level, multiple: true, collection: ["Mild", "Moderate", "Severe", "Profound"], input_html: { value: ["Mild"] }

更新:

我认为这个问题与使用select2到https://github.com/platanus/activeadmin_addons

有关

3 个答案:

答案 0 :(得分:0)

您应该使用活跃管理员的as: :select填充选择框

f.input :hearing_loss_level, as: :select, multiple: true, collection: ["Mild", "Moderate", "Severe", "Profound"] ,input_html: { value: ["Mild"] }

答案 1 :(得分:0)

试试这个......

in model

DATA = ["Mild", "Moderate", "Severe", "Profound"] #whatsoever

然后使用activeadmin:

f.input :hearing_loss_level, as: :select, multiple: true, collection: -> { ModelName::DATA }, input_html: { value: ["Mild"] }

希望这对你有用。

答案 2 :(得分:0)

以下是最终为我工作的内容:

管理模式

filter :hearing_loss_level_cont, collection: ["Mild", "Moderate", "Severe", "Profound"], as: :select, label: 'Hearing Loss Level'

form do |f|
  f.inputs "Form Factor Details" do
    f.input :hearing_loss_level, as: :select, multiple: true, collection: ["Mild", "Moderate", "Severe", "Profound"], input_html: {style:'width:80%'}
  end
  f.actions
  render partial: 'administration/shared/multiple_select', locals: {
    editing: 'form_factor', 
    context: 'hearing_loss_level',
    selected: f.object.hearing_loss_level ? JSON.parse(f.object.hearing_loss_level).reject(&:empty?) : []
  }
end

controller do
  def update
    params["form_factor"]["hearing_loss_level"] = params["form_factor"]["hearing_loss_level"].reject(&:empty?)
    super
  end
end

<强>给药/共享/ multiple_select

<script>
  $(document).ready( function () {
    $("#<%= editing %>_<%= context %>").select2('val', <%= raw selected %>)
  })
</script>