活跃的管理员有很多直通关系问题
Active admin has many through relation issue
我有 3 个模型
Fruit.rb
has_many :seeds, through :fruit_seed
has_many :fruit_seeds
Seed.rb
has_many :fruits, through :fruit_seed
has_many :fruit_seeds
fruit_seed.rb
belongs_to :fruit
belongs_to :seed
水果种子田table(fruit_id, seed_id)
为 fruit_seed
创建了活跃的管理资源
因为默认情况下我可以 select 一个水果,一个种子从下拉菜单中保存。
但我需要保存一个水果 has_many 种子(将种子列表保存为联合 table 水果种子中的数组)这意味着多个 select 用于保存种子数组。
探索了官方文档但无法解决。
在你的fruit.rb文件中,
accepts_nested_attributes_for :seeds, allow_destroy: true
然后在 activeadmin 的 fruit.rb
文件表格中,
form do |f|
f.semantic_errors *f.object.errors.keys
f.inputs "Fruit attributes" do
f.input :title
# your fruit model attributes
end
f.inputs "Fruit seeds" do
f.has_many :seeds do |s|
s.input :seed_name # whatever you want to take as input
end
end
f.actions
end
如果您使用的是rails 4+,则会出现强参数问题。在这种情况下,在 activeadmin 的 fruit.rb
文件
中使用 permit_params
permit_params :fruit_name, seeds_attributes: [:id, :seed_name, :_destroy, :fruit_id]
我有 3 个模型
Fruit.rb
has_many :seeds, through :fruit_seed
has_many :fruit_seeds
Seed.rb
has_many :fruits, through :fruit_seed
has_many :fruit_seeds
fruit_seed.rb
belongs_to :fruit
belongs_to :seed
水果种子田table(fruit_id, seed_id)
为 fruit_seed
创建了活跃的管理资源因为默认情况下我可以 select 一个水果,一个种子从下拉菜单中保存。
但我需要保存一个水果 has_many 种子(将种子列表保存为联合 table 水果种子中的数组)这意味着多个 select 用于保存种子数组。
探索了官方文档但无法解决。
在你的fruit.rb文件中,
accepts_nested_attributes_for :seeds, allow_destroy: true
然后在 activeadmin 的 fruit.rb
文件表格中,
form do |f|
f.semantic_errors *f.object.errors.keys
f.inputs "Fruit attributes" do
f.input :title
# your fruit model attributes
end
f.inputs "Fruit seeds" do
f.has_many :seeds do |s|
s.input :seed_name # whatever you want to take as input
end
end
f.actions
end
如果您使用的是rails 4+,则会出现强参数问题。在这种情况下,在 activeadmin 的 fruit.rb
文件
permit_params
permit_params :fruit_name, seeds_attributes: [:id, :seed_name, :_destroy, :fruit_id]