ActiveAdmin has_many 通过关系不更新参数 Rails
ActiveAdmin has_many through relationship not updating param Rails
我正在努力创建 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
非常奇怪,这如何让 select 框没有问题,但它会翻转复选框。尽管如此,我还是很满意这个解决方案。
我正在努力创建 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
非常奇怪,这如何让 select 框没有问题,但它会翻转复选框。尽管如此,我还是很满意这个解决方案。