我正在使用Simple form构建一个表单,其中我必须手动遍历作为HABTM关系候选的记录集合。我必须做一个手动循环,因为我需要在表中呈现关于记录的额外信息,所以我不能使用Simple Form的collection_check_boxes
。
模型:
class Foo < ActiveRecord::Base
has_and_belongs_to_many :bars
end
class Bar < ActiveRecord::Base
has_and_belongs_to_many :foos
end
View so far (HAML):
= f.simple_for_form @foo do |f|
%table
- @bars.each do |bar|
%tr
%td
# Line of interest:
= f.input :bar_ids, as: :boolean, label: bar.name
%td
= bar.additional_information
我如何告诉简单的形式,f.input :bar_ids
是一个数组的一部分,以便它将命名字段foo[bar_ids][]
和分配bar.id
的值?
事实证明,这是相当简单的,感谢2012年的这篇文章。
= f.simple_for_form @foo do |f|
%table
= f.collection_check_boxes :bars, @bars, :id, :name do |builder|
%tr
%td
= builder.check_box
= builder.label
%td
= builder.object.additional_information
可以测试
f。输入:bar_ids, as::boolean, label: bar.name, as::复选框