0

我有一个模型传感器,它has_manyaccepts_nested_attributes_for另一个模型手表有关系。在更新传感器的表格中,我有以下内容

<%= sensor_form.fields_for :watches do |watches_form| %>
   <%= watches_form.label :label %><br />
   <%= watches_form.text_field :label %>
<% end %>

这是为了允许编辑已经创建的属于 Sensor 的 Watches。

这个调用吐出表单输入,如下所示:

<input name="sensor[watches_attributes][0][label]" ... />
<input name="sensor[watches_attributes][0][id]" ... />

当这个被提交时,params传感器控制器中的对象会得到一个类似的关联

"sensor" => {
  "id"=>"1",
   "watches_attributes"=> { 
     "0"=>{"id" => "1", "label" => "foo"},
     "1"=>{"id" => "2", "label" => "bar"}
   }
}

对于一个has_many, accepts_nested_attributes_forupdate@sensor.update_attributes调用,似乎该属性键确实必须映射到一个数组。

从我在示例中看到的内容来看has_manyaccepts_nested_attributes_for、 和的组合sensor_form.fields_for应该允许我将结果params对象直接传递给@sensor.update_attributes并按预期更新每个相关对象。相反,传感器发生,没有错误,但观察对象没有更新(因为"watches_attributes"映射到哈希而不是数组?)我错过了什么吗?

4

1 回答 1

0

从 rails 3 你还需要添加

attr_accessible :watches_attributes

到你的传感器类

于 2013-09-29T12:56:16.647 回答