Ruby on rails 如何在rails中用json更新嵌套的_属性?

Ruby on rails 如何在rails中用json更新嵌套的_属性?,ruby-on-rails,ruby-on-rails-3,ruby-on-rails-3.1,ruby-on-rails-3.2,nested-attributes,Ruby On Rails,Ruby On Rails 3,Ruby On Rails 3.1,Ruby On Rails 3.2,Nested Attributes,我有 类测试

我有

类测试

类示例
现在,当我创建新的价值时,如下所示

curl-H'内容类型:application/json'-H'接受:application/json' -X POST<代码>http://localhost:3000/tests.json
-d“{\”test\”:{\“name\”:\“xxxxxx\”, \“示例\u属性\:[{\'username\':\'yyyyyyyy\',\'age\':\'25\'}]}”

它会为测试创建一个新值,也会在样本表中创建一个条目,但在更新时会更新测试表,而在样本表中创建一个新条目。那么我怎样才能更新当前条目呢


请帮助我。

您需要在更新记录()的哈希中传递
id

对于没有
id
键的每个散列,将实例化一个新记录

member.attributes = {
  name: 'Joe',
  posts_attributes: [
    { id: 1, title: '[UPDATED] An, as of yet, undisclosed awesome Ruby documentation browser!' },
    { id: 2, title: '[UPDATED] other post' }
  ]
}

是的,你完全正确。我早些时候就试过了,结果成功了,忘了贴答案。无论如何,谢谢你。
 class Sample < ActiveRecord::Base
    belongs_to :tests
 end
class Member < ActiveRecord::Base   
  has_many :posts   
  accepts_nested_attributes_for :posts
end
member.attributes = {
  name: 'Joe',
  posts_attributes: [
    { id: 1, title: '[UPDATED] An, as of yet, undisclosed awesome Ruby documentation browser!' },
    { id: 2, title: '[UPDATED] other post' }
  ]
}
params = { member: {
  name: 'joe', posts_attributes: [
    { title: 'Kari, the awesome Ruby documentation browser!' },
    { title: 'The egalitarian assumption of the modern citizen' }
  ]
}}