假设我有一个像这样的输入字段:
<%= f.input :parents, collection: @nodes, as: :check_boxes, label: "Parents" %>
我希望发生的是现有节点已经@nodes
的任何node.parents
,而不是将其复选框显示为空,我希望它中有一个复选框。< / p>
这个想法是我应该能够取消选中它们并检查其他人来更改现有节点的父节点。
答案 0 :(得分:1)
您可以使用checkbox collection builder。它允许您为每个呈现的复选框添加逻辑,因此您只需要一个简单的模型函数来检查父项。像这样:
<%= collection_check_boxes(:node, :node_ids, @nodes, :id, :node_name) do |b| %>
<% isParent = true %>
<%= b.label { b.check_box(checked:isParent) } %>
<%end%>
编辑 - 使用simple_form:
<%= simple_form_for @node do |f| %>
<%= f.input :parents,
:collection => @nodes,
:as => :check_boxes,
:checked => @node.parent_ids #array of node ids for parents
%>
<% end %>
然后使用模型函数检索父节点id的数组。