![](/img/trans.png)
[英]How to create a model and its associations in one request using Ember.js and Rails?
[英]Ember.js / Rails and associations: how to submit back to Rails a record and its associations?
假設您有兩個帶關聯的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
感謝ActiveModel :: Serializer,很容易將現有的嵌套對象從Rails發送到Ember.js。 但我找不到在Ember中創建這樣的嵌套對象的方法,並將它們發送回Rails以實現持久性。 我的理解是Ember需要發回類似的東西:
"moo"=> {
"foos_attributes"=>{"0"=>{"name" => ...}},
"name" => ...
}
是否有“標准”方法可以做到這一點? 我已經看到了關於這個主題的一些“舊”問題,但沒有真正的解決方案,而且由於Ember已經發生了如此大的變化,我認為現在可能有一種簡單的方法(不更改Rest Adapter本身)。
非常感謝,PJ
我剛剛修改了params,然后將它們交給create
或update_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.