简体   繁体   中英

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

Say you have two Rails models with association:

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. But I can't find a way to create such nested objects in Ember and send them back to Rails for persistence. My understanding is Ember needs to send back something like:

"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).

Thanks a lot, PJ

I have just modified the params before handing them to create or update_attributes methods like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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