简体   繁体   English

Ember.js / Rails和协会:如何向Rails提交记录及其关联?

[英]Ember.js / Rails and associations: how to submit back to Rails a record and its associations?

Say you have two Rails models with association: 假设您有两个带关联的Rails模型:

class Foo < ActiveRecord::Base
  attr_accessible :name

  belongs_to :moo
end

class Moo < ActiveRecord::Base
  attr_accessible :name

  has_many :foos
  accepts_nested_attributes_for :foos
end

Thanks to ActiveModel::Serializer it is easy to send existing nested objects from Rails to Ember.js. 感谢ActiveModel :: Serializer,很容易将现有的嵌套对象从Rails发送到Ember.js。 But I can't find a way to create such nested objects in Ember and send them back to Rails for persistence. 但我找不到在Ember中创建这样的嵌套对象的方法,并将它们发送回Rails以实现持久性。 My understanding is Ember needs to send back something like: 我的理解是Ember需要发回类似的东西:

"moo"=> {
   "foos_attributes"=>{"0"=>{"name" => ...}},
   "name" => ...
}

Is there a "standard" way to do that? 是否有“标准”方法可以做到这一点? I have seen a few "old" questions on this subject but no real solution, and since Ember has been changing so much I thought there might be a simple way to do it now (without changing the Rest Adapter itself). 我已经看到了关于这个主题的一些“旧”问题,但没有真正的解决方案,而且由于Ember已经发生了如此大的变化,我认为现在可能有一种简单的方法(不更改Rest Adapter本身)。

Thanks a lot, PJ 非常感谢,PJ

I have just modified the params before handing them to create or update_attributes methods like this: 我刚刚修改了params,然后将它们交给createupdate_attributes方法,如下所示:

  line_items = params[:invoice].delete(:line_items) #Ember format
  params[:invoice][:line_items_attributes] = line_items

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM