1

我正在我的模型中创建一个虚拟属性:

def entities
  @entities = Array.new()
  @entities.push(self.contact.name)
  @entities.push(self.contact.partner.name) if self.contact.partner
  @entities.push('Joint') if self.contact.partner
  @entities
end

然后在我的表单中,我试图从嵌套数组中使用这个数组。我正在使用简单的形式,所以它看起来像这样

<%= f.input :ownership, collection: :entities, :include_blank => false, :label => false %>

然而,这给了我一个错误:

undefined method `to_a' for :entities:Symbol

如果我创建了一个数组,我不明白为什么它不呈现为一个数组。我错过了什么?

4

1 回答 1

2

您不能:entities用作集合:

<%= f.input :ownership, collection: :entities ...%>

那是行不通的。该错误表明 Simple Form 正在尝试将参数转换:entities为数组,这会导致错误。

你需要给它一个实际的集合:

<%= f.input :ownership, collection: @object.entities ... %>
于 2012-10-07T01:14:01.947 回答