将选中的数组传递给rails aplication中表单视图上的select2字段

时间:2016-09-22 18:10:34

标签: ruby-on-rails ruby forms

在我的ruby on rails应用程序中,我有一个名为@card的类,其中包含很少的select2标记字段,例如:

<%= f.select :tag_location, options_from_array_for_select(@domain.tag_location), {:include_blank => "Escolha uma opção"} , {class: 'form-control', multiple: true} %>

@card.tag_location的值可以是:["", "Asa Norte", "Asa Sul"]["", "Lago Norte", "Asa Sul"]

我试图在UPDATE视图中将此字段标记为已选中,并显示数组的值:

我试过了:

1)<%= f.select :tag_location, options_from_array_for_select(@domain.tag_location, :selected => params[:tag_location]), {:include_blank => "Escolha uma opção"} , {class: 'form-control', multiple: true} %>

2)<%= f.select :tag_location, options_from_array_for_select(@domain.tag_location, params[:tag_location]), {:include_blank => "Escolha uma opção"} , {class: 'form-control', multiple: true} %>

3)<%= f.select :tag_location, options_from_array_for_select(@domain.tag_location), {:include_blank => "Escolha uma opção"} , {class: 'form-control', multiple: true, data: {selected: @card.tag_location}} %>

3)<%= f.select :tag_location, options_from_array_for_select(@domain.tag_location), {:include_blank => "Escolha uma opção"} , {class: 'form-control', multiple: true, data: {selected: @card.tag_location.drop(1)}} %>

我找不到让所选值有效的方法,可以帮助一些人;)

由于

2 个答案:

答案 0 :(得分:1)

我认为您正在寻找options_from_collection_for_select而不是options_from_array_for_select,它会根据传递的集合为您创建一组<option></option>标记。

你可以这样下拉:

<%= f.select :tag_location, @domain.tag_location, {:include_blank => "Escolha uma opção"} , {class: 'form-control', multiple: true} %>

答案 1 :(得分:0)

找到答案,不明白为什么,但它有效

<%= f.select :tag_location, options_from_array_for_select(@domain.tag_location, @card.tag_location), {:include_blank => "Escolha uma opção"} , {class: 'form-control', multiple: true} %>
相关问题