简体   繁体   中英

How do I return the object from a collection with simple_form?

I have a simple_form input field that looks like this:

<%= f.input :parents, collection: (@node.family_tree.nodes - @node.parents - [@node]).uniq, label: "Parent 1" %>

But this produces HTML like this:

<option value="13">Jack</option>

Where the value is the ID of the object selected from the collection.

However, I would like for the entire object to be returned as an AR object or an array. 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.

So, using these examples, how do I return the entire node object for id=13 rather than just the 13 ?

It seems like you should be using simple_form's f.association rather than f.input for dealing with relations.

See https://github.com/plataformatec/simple_form#associations

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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