2014-01-07 3 views
4

여러 선택 값을 저장하려고 할 때 레일스 도우미 선택에 의해 생성 된 다중 선택 필드가 있습니다.선택 항목 : 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을 넣으려고했으나 아무 것도 변경하지 않았습니다. :/

편집 2 : 여러 selcted 필드는 내가 선택한 경우 옵션을 선택 사항으로 표시하지 않는 것으로 나타났습니다.

{"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 필드 레일 4.0.2를 사용하여 값 "available_type"=>["", "MBB", "Prepaid"]

임을 가지고 있으며, 여기에 내 강력한 parametres입니다 : 내가 수동으로 실시 선택된 속성을 추가하면 내가이 parametres를 얻을

# 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

질문에 매개 변수가 추가되었습니다. 나는 또한 옵션이 수동으로 "selected"로 표시되지 않으면 available_type parametres가 sendt를 얻지 못한다는 사실을 발견했다. 이게 정상인가? –

+0

유효성 검사 또는 콜백이 필요합니까? 어떤 레일 버전을 사용하고 있습니까? –

답변

8

마침내 대답을 찾았습니다!

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 

그럼 난에 필요한 :

이 문제는 배열 때문에 등의 PG의 콤보와 레일 (4)

처음에는 텍스트 컬럼에 문자열에서 열을 변환하는 데 필요한 표시했다 그래서 같은 단말 제어부의 배열로 강한 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 => []}

변경 사항이 문제를 해결했습니다. :)