[英]How do I return the object from a collection with simple_form?
I have a simple_form input field that looks like this: 我有一个simple_form输入字段,如下所示:
<%= f.input :parents, collection: (@node.family_tree.nodes - @node.parents - [@node]).uniq, label: "Parent 1" %>
But this produces HTML like this: 但这会产生如下的HTML:
<option value="13">Jack</option>
Where the value
is the ID
of the object selected from the collection. 其中
value
是从集合中选择的对象的ID
。
However, I would like for the entire object to be returned as an AR object or an array. 但是,我希望将整个对象作为AR对象或数组返回。 For instance, I would love to be able to get something like this:
例如,我很希望能够得到这样的东西:
<%= f.input :parents, collection: (@node.family_tree.nodes - @node.parents - [@node]).uniq, value_method: :parents, label: "Parent 1" %>
Produces this: 产生这个:
<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>
But using value_method: :self
doesn't work. 但是使用
value_method: :self
不起作用。
So, using these examples, how do I return the entire node object for id=13
rather than just the 13
? 因此,使用这些例子中,我怎么返回整个节点对象
id=13
,而不是仅仅是13
?
It seems like you should be using simple_form's f.association
rather than f.input
for dealing with relations. 似乎您应该使用simple_form的
f.association
而不是f.input
来处理关系。
See https://github.com/plataformatec/simple_form#associations 参见https://github.com/plataformatec/simple_form#associations
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.