簡體   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