2010-02-25 27 views
1

好时,所以我有这些2类HashWithIndifferentAccess保存对象的阵列

class Interface < ActiveRecord::Base 
    belongs_to :hardware  
end 

class Hardware < ActiveRecord::Base 
    has_many :interfaces 
end 

我有一个表格预定义@ hardware.interfaces,其包括接口的阵列,其处理如下

<%= text_field_tag "hardware[interfaces][][name]",interface.name %> 
<%= text_field_tag "hardware[interfaces][][ip]",interface.ip %> 

现在我尝试做...

@hardware = Hardware.find(params[:id]) 
@hardware.update_attributes(params[:hardware]) 

,并引发错误

Interface(#37298420) expected, got HashWithIndifferentAccess(#24204840) 

可能有人给我介绍什么是怎么回事?以及如何解决这个问题?

回答

2

update_attributes方法更新模型属性..你要更新另一个模型属性(接口类),你想使用嵌套的表格& accepts_nested_attributes_for

- 你可以在这个guide

+0

也就是说怎么看正是我正在寻找的!谢谢! – Taka 2010-02-25 09:52:55

+0

不要忘记在表单中使用interface_attributes而不是interfaces []。 – nanda 2010-02-25 11:32:45

+0

会做。谢谢! – Taka 2010-02-26 00:20:57