[英]How do I tell my simple_form object to return an array with just the selected IDs and not any empty values?
[英]How do I return the object from a collection with simple_form?
我有一個simple_form輸入字段,如下所示:
<%= f.input :parents, collection: (@node.family_tree.nodes - @node.parents - [@node]).uniq, label: "Parent 1" %>
但這會產生如下的HTML:
<option value="13">Jack</option>
其中value
是從集合中選擇的對象的ID
。
但是,我希望將整個對象作為AR對象或數組返回。 例如,我很希望能夠得到這樣的東西:
<%= f.input :parents, collection: (@node.family_tree.nodes - @node.parents - [@node]).uniq, value_method: :parents, label: "Parent 1" %>
產生這個:
<option value="[#<Node id: 36, family_tree_id: 2, created_at: "2015-01-28 23:19:28", updated_at: "2015-01-28 23:19:28", name: "Mesty", ancestry: "13/35", ancestry_depth: 0, max_tree_depth: 0>]">Resty</option>
但是使用value_method: :self
不起作用。
因此,使用這些例子中,我怎么返回整個節點對象id=13
,而不是僅僅是13
?
似乎您應該使用simple_form的f.association
而不是f.input
來處理關系。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.