2015-04-22 62 views
2

我正在通過在活動管理中創建一個has_many:through關係來工作。下面是模型因爲他們的立場:ActiveAdmin has_many通過關係不更新param Rails

class Category < ActiveRecord::Base 
    has_many :subcategories 
end 

class Subcategory < ActiveRecord::Base 
    has_many :product_in_subcategories 
    has_many :products, through: :product_in_subcategories 
    accepts_nested_attributes_for :product_in_subcategories, :allow_destroy => true 

    belongs_to :category 
end 

class Product < ActiveRecord::Base 
    has_many :product_in_subcategories 
    has_many :subcategories, through: :product_in_subcategories 
    accepts_nested_attributes_for :product_in_subcategories, :allow_destroy => true 
end 

class ProductInSubcategory < ActiveRecord::Base 
    belongs_to :product 
    belongs_to :subcategory 
end 

在ActiveAdmin我有permit_params,形成像這樣:

ActiveAdmin.register Product do 
    # note some params that are product only have been removed for simplicity 
    permit_params :name, subcategory_id:[:id], product_in_subcategories_attributes: [:id, :subcategory_id, :product_id, :_create, :_update] 

    form do |f| 
     f.inputs 

     f.has_many :product_in_subcategories do |s| 
      s.input :subcategory_id, :as => :check_boxes, :collection => Subcategory.all 
     end 

     f.actions 
    end 
end 

形式填充爲應,拯救一切除了subcategory_id。如果我在數據庫中輸入了一個合適的子類別標識,那麼該框會顯示在編輯時檢查。

節省給予時消息:

Unpermitted parameters: subcategory_id 

然而,似乎它試圖與產品,對此沒有一個subcategory_id提交此。關於我在這裏做錯了什麼的想法?這讓我瘋狂,我已經閱讀了所有我能找到的東西。我真的很想明白我做錯了什麼。謝謝。

回答

2

在花了很多時間在這個之後,我找不到一個合適的解決方案,除了這個,其實非常好。它其實不是來自我預想的解決方案非常不同:

上面的代碼中的唯一變化是在ActiveAdmin提出:

ActiveAdmin.register Product do 
    # note some params that are product only have been removed for simplicity 
    permit_params :name, product_in_subcategories_attributes: [:id, :subcategory_id, :product_id, :_create, :_update] 

     form do |f| 
     f.inputs 

     f.has_many :product_in_subcategories do |s| 
      s.input :subcategory_id, :as => :select, :collection => Subcategory.all 
     end 

     f.actions 
     end 
end 

很奇怪,怎麼這使得沒有問題一個選擇框,但它翻轉超出複選框。儘管如此,我對解決方案感到滿意。

+0

任何解決方案與複選框? –

+1

說實話,我推出了自己的寶石,這樣管理員就可以快速和無需DSL定製。你可以在[SkinnyAdmin](https://www.skinnyadmin.org)找到它。 ActiveAdmin沒什麼問題,但DSL對我來說很頭疼。 – user1572597