2014-01-07 39 views
4

我试图保存多个选定的值形成一个由rails helper select生成的多选字段。选择:multiple => true不保存值

<div class="form-group"> 
    <%= f.label :available_type, "Available in category" %><br> 
    <%= f.select :available_type, options_for_select(Setting.subscription_type, @terminal.available_type), { }, { class: "form-control", :multiple => true, :size => 5 } %> 
    </div> 

这使得这样的(所选择的值是从先前的尝试没有 “:多个=> true” 属性完美的作品):

<select class="form-control" id="terminal_available_type" multiple="multiple" name="terminal[available_type][]" size="5"> 
<option value="Postpaid">Postpaid</option> 
<option value="MBB">MBB</option> 
<option selected="selected" value="Prepaid">Prepaid</option> 
</select> 

enter image description here

任何帮助是赞赏。 :)

编辑: 我试图把serialize :available_type在我的终端型号,dident改变任何东西。 : -/

编辑2: 我注意到,多选字段不标记选项时,我标记它们。如果我添加manualy选定的属性我得到这些参数应用:

{"utf8"=>"✓", "_method"=>"patch", "authenticity_token"=>"RrwWlKk8XlGeC+dTu/w6oSM68e9LcbUFJWTI+eRS9mI=", "terminal"=>{"inndate"=>"2015-01-13", "outdate"=>"", "brand_id"=>"2", "name"=>"iPhone 5c", "available_type"=>["", "MBB", "Prepaid"], "product_number"=>"3r2342", "ean_code"=>"", "navision_nb"=>"324234", "cost_price_map"=>"3200.0", "manual_price"=>"", "sales_info"=>"Just sell!"}, "commit"=>"Submit", "action"=>"update", "controller"=>"terminals", "id"=>"2"} 

的available_type领域具有使用Rails 4.0.2的值"available_type"=>["", "MBB", "Prepaid"]

林,这里是我的强烈参数应用:

# Never trust parameters from the scary internet, only allow the white list through. 
def terminal_params 
    params.require(:terminal).permit(:inndate, :outdate, :brand_id, :name, :product_number, :navision_nb, :cost_price_map, :manual_price, :sales_info, :available_type) 
end 
+0

你可以发布你的控制器中收到的参数吗? –

+0

为问题添加参数。我还注意到,如果选项未被手动标记为“已选”,则available_type参数不会被sendt获取。这是正常的吗? –

+0

任何验证或回调?您使用的是哪种版本的导轨? –

回答

8

Finaly找到答案!

此问题是PG的一个组合和Rails 4.

首先我需要列从一个字符串转换为文本列标记为阵列像这样:

class ChangeAvailableTypeOnTerminals < ActiveRecord::Migration 
    def up 
    change_column :terminals, :available_type, :text, array: true, default: [] 
    end 

def down 
    change_column :terminals, :available_type, :string 
    end 
end 

然后我需要对待强parametre如在终端控制器的阵列,像这样:

# Never trust parameters from the scary internet, only allow the white list through. 
def terminal_params 
    params.require(:terminal).permit(:inndate, :outdate, {:available_type => []}, :brand_id, :name, :product_number, :navision_nb, :cost_price_map, :manual_price, :sales_info) 
end 

更spesific:

:available_type更改为{:available_type => []}

这解决了我的问题。 :)