我正在使用activeadmin,它内置了formtastic,因为许多使用它的人都知道。我有一个名为Project的模型,它与ProjectResources有许多关联。
我在Project的活动管理员中的自定义“编辑”和“创建”表单看起来像这样。
form do |f|
f.inputs "Project" do
f.input :name, :input_html => { :readonly => true }
end
f.inputs "Resources" do
f.input :id, :label => "Selected Resources",
:as => :check_boxes,
:multiple => true,
:collection => ProjectResource.all,
:selected => @resources
end
f.buttons
end
我的复选框渲染得很好,此时我没有收到任何错误。您可能已经猜到的问题是,在呈现“编辑”页面时,如果项目已将ProjectResource作为关联,我希望将复选框区域中的项目显示为“已选择”。
现在,复选框都显示取消选择状态。我使用的是最新版本的activeadmin,formtastic安装了以下版本。 (2.2.0,2.1.1,2.1.0,2.0.2,1.2.4)
不确定activeadmin在此时使用的版本。我的猜测是最新版本。
答案 0 :(得分:9)
对我来说,简单:
ActiveAdmin.register Subscription do
form do |f|
f.inputs do
f.input :users, as: :check_boxes
# other fields...
end
f.buttons
end
end
正常工作。
更多代码:
-User class
class User < ActiveRecord::Base
has_and_belongs_to_many :users
attr_accessible :fields...
end
-Subscription Class
class Subscription < ActiveRecord::Base
has_and_belongs_to_many :subscriptions
attr_accessible :fields...
end
PS我使用的是ActiveAdmin 0.4.2和Formtastic 2.0.2。