1

我試圖建立一個標準的關係如下:軌道4有許多通過與主動管理

class Category < ActiveRecord::Base 
    has_many :post_categories 
    has_many :posts, :through => :post_categories 
    accepts_nested_attributes_for :post_categories 
end 

class Post < ActiveRecord::Base 
    has_many :post_categories 
    has_many :categories, :through => :post_categories 
    accepts_nested_attributes_for :post_categories 
    attr_accessor :category_ids 
end 

class PostCategory < ActiveRecord::Base 
    belongs_to post 
    belongs_to category 
end 

我使用ActiveAdmin和需要設置複選框來描述這種關係。 我已經嘗試了許多不同的方法來保存複選框。這裏是我的管理post.rb文件:

ActiveAdmin.register Post do 

    permit_params :content, category_ids: [] 

    form do |f| 
    f.inputs # Include the default inputs 
    f.inputs "Categories" do 
     f.input :categories, as: :check_boxes, collection: Category.all 
    end 
    f.actions # Include the default actions 
    end 
end 

我已經嘗試了不同的許可證PARAMS如

permit_params :content, :categories 
permit_params :content, post_categories_attributes: [:id, :post_id, :category_id] 
permit_params :content, category_ids: [:id] 

數據庫設置如圖所示的軌道教程,和關係似乎在其他地方工作除了從activeadmin保存。我甚至試圖用param.permit!來允許所有參數,但仍然沒有運氣。

我發現許多看似相同的問題,但很多人給出不同的答案,似乎沒有任何工作。

出了什麼問題?

+0

http://stackoverflow.com/a/24486212/3199803此解決方案有效,但似乎應該有一個更簡潔的方法。 對於同一個問題的其他解決方案不適合我。 – BreadnButter 2014-09-28 22:34:53

+0

創建操作開始時日誌文件中的內容是什麼? – nistvan 2014-09-29 11:44:08

回答

0

小遲,但這個問題是第一次用Google搜索我,所以我認爲它可以幫助到其他的「Google員工」

#models/post.rb 

accepts_nested_attributes_for :categories#, allow_destroy: true 

#AA Post conf file 

permit_params :content, category_ids: []