我有两种型号:
Model: Stores => Relavant fields: id, retailer_id
Model: Retailer => Relavant fields: name, id
我试图创建的表单是一个activeadmin表单,它查看零售商名称,然后在check_boxes中返回与该名称匹配的商店列表。我想要的是用户选择他想要与零售商关联的商店,然后点击update,这将基本上用当前零售商记录更新商店模型中的retailer_id。
如果创建一个新的零售商,我希望复选框显示retailer_id为空的所有商店。
在我的零售商模型中,我添加了
has_many :stores
accepts_nested_attributes_for :stores
在我的商店模型中,我添加了
belongs_to :retailer
这是我目前在零售商活动管理表单中的内容
ActiveAdmin.register Retailer do
action_item do
link_to "View unassigned stores", "/admin/unassigned_stores"
end
form :html => { :enctype => "multipart/form-data" } do |f|
f.inputs do
f.input :name
f.input :description, :as => :text
f.input :photo, :label => "Retailer Logo", :as => :file, :hint => image_tag(retailer.photo.url)
f.input :retailer_id, :for => :Stores, :as => :check_boxes, :collection => Store.find(:all, :conditions => ["retailer_id is null and (name like ? or name_2 like ?) ", "%#{retailer.name}%", "%#{retailer.name}%"]), :label => "Assign Stores to retailer"
end
f.buttons
完结束
经过几个小时的搜寻,终于找到了这个小宝石。ActiveAdmin--显示嵌套表单的复选框列表,而不是要添加项目的表单
基本上,我将:retailer_id更改为:stores,并将:store_ids添加为attr:accessible,这使我可以做我想做的事情。