Ruby on rails Rails 3如何允许在没有_属性指定的情况下传递嵌套属性

Ruby on rails Rails 3如何允许在没有_属性指定的情况下传递嵌套属性,ruby-on-rails,ruby,ruby-on-rails-3,activerecord,Ruby On Rails,Ruby,Ruby On Rails 3,Activerecord,当使用accepts_nested_attributes_for时,我希望传递“child”,而不是传递“child_attributes”。我很确定,若我在控制器中加入大量的逻辑来创建记录和子项,我就能完成这项工作。然而,为了保持我的控制器干净和逻辑,在本例中的模型,我想知道如何在执行POST或PUT时切换rails 3以使用此语法 { "name": "test", "child_attributes": [ { "id": 1, "name": "t

当使用accepts_nested_attributes_for时,我希望传递“child”,而不是传递“child_attributes”。我很确定,若我在控制器中加入大量的逻辑来创建记录和子项,我就能完成这项工作。然而,为了保持我的控制器干净和逻辑,在本例中的模型,我想知道如何在执行POST或PUT时切换rails 3以使用此语法

{
  "name": "test",
  "child_attributes": [
    {
      "id": 1,
      "name": "test_child_update"
    },
    {
      "name": "test_child_create"
    }
}
相当于

{
  "name": "test",
  "child": [
    {
      "id": 1,
      "name": "test_child_update"
    },
    {
      "name": "test_child_create"
    }
}

显然,这是不可能做到的。

属性后缀对JSON请求和响应没有任何价值,但要在模型层中消除它,您必须对ActiveRecord进行monkey patch。每个人都讨厌猴子修补ActiveRecord关系

在控制器层做这个怎么样

@comment = Comment.new(attributify(:comment))

# snip

# ApplicationController

def attributify()
  # Now I'll go and write this!
end

编辑:完成。控制器mixin在这里:

我可以问一下为什么要更改它吗?这种约定是有原因的,因为
child
属性实际上是指
child
对象的集合,而不是
child
对象的属性。
child\u属性
属性用于阐明意图。我将在这里涉水。传入的对象的类型应足以确定目的。另外,由于JSON中没有类,所以使用
\u属性实际上并不能清除任何内容。我认为堆栈中只有您承认某些事情无法完成