2014-01-12 16 views
1

我試圖修改我的活動管理員用戶窗體來修改用戶的角色。角色是通過role_assignments表進行的多態關聯。指定與Active Admin的新多態關聯

當我提交表單時,角色沒有更新,我猜是因爲關聯的屬性被保護,我沒有正確使用permit_params。

ActiveAdmin.register User do 
    permit_params :email, :newsletter_subscription, :password, :password_confirmation, 
       role_assignment_attributes: [:role_id, :user_id, :_destroy, :_create] 
    ... 
end 

任何想法?附加信息:

模式:

create_table "role_assignments", force: true do |t| 
    t.integer "role_id" 
    t.integer "user_id" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
end 

的類很簡​​單:

class RoleAssignment < ActiveRecord::Base 
    belongs_to :user 
    belongs_to :role 
end 

class Role < ActiveRecord::Base 
    has_many :role_assignments 
    has_many :users, through: :role_assignments 
    belongs_to :user 
end 

這工作找到並給了我一個很好的形式修改角色。用戶的活動管理員表單:

form do |f| 
    f.inputs "Admin Details" do 
    f.input :email 
    f.input :newsletter_subscription, :as => :radio 
    f.input :roles, :as => :check_boxes 
    end 
end 

但是,當我提交表單時,沒有分配任何內容。

回答

1

docs illustrate,你需要通過:id爲允許使用的參數內:role_assignment_attributes

permit_params :email, :newsletter_subscription, :password, :password_confirmation, 
       role_assignment_attributes: [:id, :role_id, :user_id, :_destroy] 

注意,我刪除了:_create PARAM,因爲我從來沒有見過這樣的事情,所以我假設它被無意中添加了。