[英]Rails 4 has many through with active admin
我正在尝试建立如下的标准关系:
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
我尝试了不同的许可证参数,例如
permit_params :content, :categories
permit_params :content, post_categories_attributes: [:id, :post_id, :category_id]
permit_params :content, category_ids: [:id]
该数据库的设置如rails教程中所示,并且该关系似乎在其他地方起作用,除了从activeadmin中保存。 我什至尝试使用param.permit!
允许所有参数,但仍然没有运气。
我发现许多看似相同的问题,但许多给出不同的答案,而且似乎无济于事。
怎么了?
来晚了,但是这个问题对我来说是第一个搜索到的谷歌,所以我认为这对其他“谷歌搜索者”会有所帮助
#models/post.rb
accepts_nested_attributes_for :categories#, allow_destroy: true
#AA Post conf file
permit_params :content, category_ids: []
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.