2017-04-19 203 views
0

我有這樣的代碼多選擇在軌嵌套屬性4

class Department < ActiveRecord::Base 
    has_many :admin_permissions 
    accepts_nested_attributes_for :admin_permissions 
end 

查看:

= ep.fields_for :department, dept_object do |dept| 
    = dept.fields_for :admin_permissions, @user.admin_permissions.new(department: dept.object) do |ap| 
     = ap.collection_select :permission_type, AdminPermission.permission_types.map{ |dp| [dp.first, dp.first.humanize] }, :first, :second, {}, { :multiple => true, class: 'form-control' } 

但在提交按鈕被按下後的參數,可以在PARAMS是這樣的:

"department_attributes"=> 
    {"company_id"=>"2", 
    "name"=>"", 
    "admin_permissions_attributes"=>{"0"=>{"permission_type"=>["", "human_resource"]}}}, 

而不是

"department_attributes"=> 
    {"company_id"=>"2", 
    "name"=>"", 
    "admin_permissions_attributes"=>{"0"=>{"permission_type"=>"hr"}, "1" => {"permission_type" => "manager"}}} 

我怎樣才能實現這與多選擇?謝謝!

回答

0

你的語法看起來是正確的,但你可以嘗試刪除:多出來的括號

ap.collection_select :permission_type, AdminPermission.permission_types.map{ |dp| [dp.first, dp.first.humanize] }, :first, :second, {}, :multiple => true

,但結果總是會在形式

"admin_permissions_attributes"=>{"0"=>{"permission_type"=>["", "human_resource", "management"]}}}

他們將不會有他們自己的ID,但會作爲列表傳遞。

+0

是否會創建單獨的對象? – user3620046

+0

不,我想你必須自己在控制器中執行update_attribute調用。但我不確定這可以與你的符號一起工作。與嵌套屬性的關係是什麼類型?你確實想爲每個傳入的選擇創建一個新的,而不僅僅是像has_and_belongs_to_many關係那樣傳遞和關聯,對嗎? – talkingfox