我有两个模型:
Model: Stores => Relavant fields: id, retailer_id
Model: Retailer => Relavant fields: name, id我尝试创建的表单是一个活动管理表单,它查看零售商名称,然后返回与check_boxes中的名称匹配的商店列表。我想要的是用户选择他想要与零售商关联的商店,并点击更新,这将基本上更新商店模型中的retailer_id与当前零售商记录。
如果创建一个新的零售商,我希望复选框显示retailer_id为空的所有商店。
在我的零售商模型中,我添加了
has_many :stores
accepts_nested_attributes_for :stores在我的商店模型中,我添加了
belongs_to :retailer这是我当前在我的零售商activeadmin表单中拥有的内容
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结束结束
发布于 2013-12-17 05:01:58
经过几个小时的搜寻,终于找到了这颗小宝石。ActiveAdmin -- Show list of checkboxes for nested form instead of a form to add items
基本上,我将attr更改为::retailer_id,并将:store_ids添加为attr:accessible,这样我就可以做我想要做的事情了。
https://stackoverflow.com/questions/20588000
复制相似问题