我正在通过事件管理中的关系创建一个 has_many: 。以下是目前的模型:
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 之外的所有内容。如果我在数据库中输入正确的 subcategory_id,该框将在编辑时显示选中状态。
保存时的消息给出:
Unpermitted parameters: subcategory_id
但是,它似乎正在尝试将其与产品一起提交,但该产品没有 subcategory_id。关于我在这里做错了什么有什么想法吗?这让我发疯,我已经阅读了我能找到的所有内容。我真的很想了解我做错了什么。谢谢。
最佳答案
在这个问题上花了很多时间之后,除了这个之外我找不到合适的解决方案,这实际上非常好。事实上,它与我设想的解决方案没有太大区别:
对上述代码的唯一更改是在 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
非常奇怪的是,这允许选择框没有任何问题,但它会翻转复选框。尽管如此,我对这个解决方案很满意。
关于ruby-on-rails-4 - ActiveAdmin has_many 通过关系不更新参数 Rails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29810223/