Rails - 创建模型及其嵌套资源

时间:2013-05-12 17:07:27

标签: ruby-on-rails rest model nested-attributes nested-resources

我正在尝试通过将以下内容传递给has_many来创建Gallery模型及其create关联。

[
  {
    "title":"Some Titler",
    "description":"",
    "date":"18-3-2012",
    "photographs":[
      {
        "title": "Some Title",
        "camera": "Canon 600D"
      },
      {
        "title": "Some Other Title",
        "camera": "Canon 600D"
      }
    ]
  }
]

没有嵌套的photographs数组,它工作正常,但有了它,我收到一个错误:

Photograph(#70242279271180) expected, got Hash(#70242248401160)

语法有什么问题?

class Gallery < ActiveRecord::Base
  attr_accessible :date, :description, :published, :title


  has_many :photographs
  accepts_nested_attributes_for :photographs

end

1 个答案:

答案 0 :(得分:1)

更改

 "photographs":[]

 "photographs_attributes":[]