繁体   English   中英

如何使用simple_form从集合中返回对象?

[英]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: &quot;2015-01-28 23:19:28&quot;, updated_at: &quot;2015-01-28 23:19:28&quot;, name: &quot;Mesty&quot;, ancestry: &quot;13/35&quot;, ancestry_depth: 0, max_tree_depth: 0>]">Resty</option>

但是使用value_method: :self不起作用。

因此,使用这些例子中,我怎么返回整个节点对象id=13 ,而不是仅仅是13

似乎您应该使用simple_form的f.association而不是f.input来处理关系。

参见https://github.com/plataformatec/simple_form#associations

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM