简体   繁体   English

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

[英]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: &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>

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.

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