我在active-admin
中有以下输入f.inputs 'Programme description' do
f.input :nationalities,
as: :select,
hint: 'Nationalität des Suchenden',
collection: all_countries,
input_html: {multiple: true, class: 'select2', style: 'width: 70%'}
end
请注意,有三个名称为programme[nationality_ids][]
的表单字段。
<li class="select input optional" id="programme_nationalities_input">
<input name="programme[nationality_ids][]" type="hidden" value="" />
<label class="label" for="programme_nationality_ids">Nationalities</label>
<input name="programme[nationality_ids][]" type="hidden" value="" />
<select class="select2"
id="programme_nationality_ids"
multiple="multiple"
name="programme[nationality_ids][]"
placeholder="Type country name"
style="width: 70%">
<option value="CH">Switzerland</option>
<option value="LI">Liechtenstein</option>
<option value="DE">Germany</option>
<option value="AT">Austria</option>
<option value="FR">France</option>
<option value="IT">Italy</option>
</select>
这些插入的隐藏表单字段会导致空参数,从而导致异常,因为空字符串转换为0
,并且找不到id=0
的对象。< / p>
如何清理这些参数,从而避免例外。
"nationality_ids"=>["", "", "FR"]
答案 0 :(得分:1)
在包含以下格式的ActiveAdmin模型中:
controller do
def create
params[:programme][:nationality_ids].reject! {|nat_id| nat_id.blank?}
create!
end
def update
params[:programme][:nationality_ids].reject! {|nat_id| nat_id.blank?}
update!
end
end