Activeadmin,复制 has_many 条记录
Activeadmin, duplicating has_many records
当我使用 ActiveAdmin 编辑一个代理时,我可以 select 一个城市并将其关联到代理。该城市已链接到该机构,但该城市在数据库中始终重复。
我的模特:
# agency.rb
class Agency < ActiveRecord::Base
has_many :agency_cities
has_many :cities, through: :agency_cities
accepts_nested_attributes_for :cities, allow_destroy: true
end
# city.rb
class City < ActiveRecord::Base
has_many :agency_cities
has_many :agencies, through: :agency_cities
end
# AgencyCity.rb
class AgencyCity < ActiveRecord::Base
belongs_to :agency
belongs_to :city
end
我阅读了Activeadmin
的文档并添加了[:id] permit_parameter
,但我仍然有问题,我很困惑。
# admin/agency.rb
ActiveAdmin.register Agency do
permit_params :name, :picture,
cities_attributes: [:id, :name, :description, :_destroy]
form do |f|
f.inputs "Agencies" do
f.input :picture, as: :file
f.input :creation_date, label: 'Creation Date'
f.input :name, label: 'Name'
end
end
f.inputs do
f.has_many :cities do |k|
k.input :name, label: 'City',
as: :select,
collection: City.all.map{ |u| "#{u.name}"}
k.input :_destroy, as: :boolean
end
end
f.actions
end
您可以在生成的 html 中检查城市 select 输入中的选项值是城市的名称(而不是 ID)。
试试这个方法:collection: City.all.map{ |u| [u.name, u.id]}
一些参考:http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html
您正试图将现有城市与代理机构相关联,因此,您应该这样做:
ActiveAdmin.register Agency do
permit_params city_ids: [] # You need to whitelist the city_ids
form do |f|
f.inputs "Agencies" do
f.input :picture, as: :file
f.input :creation_date, label: 'Creation Date'
f.input :name, label: 'Name'
f.input :cities, as: :check_boxes, checked: City.pluck(&:id) # this will allow you to check the city names that you want to associate with the agency
end
end
end
这将允许您将所选城市关联到相应的机构,而无需在数据库中创建(复制)新城市。我想这就是您要找的:-)
当我使用 ActiveAdmin 编辑一个代理时,我可以 select 一个城市并将其关联到代理。该城市已链接到该机构,但该城市在数据库中始终重复。
我的模特:
# agency.rb
class Agency < ActiveRecord::Base
has_many :agency_cities
has_many :cities, through: :agency_cities
accepts_nested_attributes_for :cities, allow_destroy: true
end
# city.rb
class City < ActiveRecord::Base
has_many :agency_cities
has_many :agencies, through: :agency_cities
end
# AgencyCity.rb
class AgencyCity < ActiveRecord::Base
belongs_to :agency
belongs_to :city
end
我阅读了Activeadmin
的文档并添加了[:id] permit_parameter
,但我仍然有问题,我很困惑。
# admin/agency.rb
ActiveAdmin.register Agency do
permit_params :name, :picture,
cities_attributes: [:id, :name, :description, :_destroy]
form do |f|
f.inputs "Agencies" do
f.input :picture, as: :file
f.input :creation_date, label: 'Creation Date'
f.input :name, label: 'Name'
end
end
f.inputs do
f.has_many :cities do |k|
k.input :name, label: 'City',
as: :select,
collection: City.all.map{ |u| "#{u.name}"}
k.input :_destroy, as: :boolean
end
end
f.actions
end
您可以在生成的 html 中检查城市 select 输入中的选项值是城市的名称(而不是 ID)。
试试这个方法:collection: City.all.map{ |u| [u.name, u.id]}
一些参考:http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html
您正试图将现有城市与代理机构相关联,因此,您应该这样做:
ActiveAdmin.register Agency do
permit_params city_ids: [] # You need to whitelist the city_ids
form do |f|
f.inputs "Agencies" do
f.input :picture, as: :file
f.input :creation_date, label: 'Creation Date'
f.input :name, label: 'Name'
f.input :cities, as: :check_boxes, checked: City.pluck(&:id) # this will allow you to check the city names that you want to associate with the agency
end
end
end
这将允许您将所选城市关联到相应的机构,而无需在数据库中创建(复制)新城市。我想这就是您要找的:-)