我無法確定使用Ember.js,Ember Data和Rails將單個窗體保持爲hasMany關聯的正確方法。客戶有許多項目。我有一個新的項目表單,它有兩個字段:項目名稱和客戶端名稱。 http://cl.ly/image/3z0P0R3M1t2u如何使用Ember Data&Rails在一個Ember.js表單中堅持hasMany關聯?
我試圖讓我的Ember.js ClientsController & ProjectsController中創建的邏輯,但我需要一些是移動到提交動作上我ProjectsNewView?
更新:我發現此issue後更新了我的代碼。我越來越近了,但Rails ProjectsController仍然沒有收到關聯的client_id。它不是控制器收到的參數的一部分。它仍然覺得我可能不會採取這種最好的方式。
型號:
Rails的
class Client < ActiveRecord::Base
attr_accessible :name
has_many :projects
accepts_nested_attributes_for :projects
validates :name, :presence => true
end
class Project < ActiveRecord::Base
attr_accessible :name, :client_id
belongs_to :client
validates :name, :presence => true
validates :client, :presence => true
end
Ember.js
App.Client = DS.Model.extend
name: DS.attr('string')
projects: DS.hasMany('App.Project', { embedded: true })
validate: ->
if @get('name') is null or @get('name') is ''
'Client requires a name.'
App.Project = DS.Model.extend
name: DS.attr('string')
client: DS.belongsTo('App.Client')
validate: ->
if @get('name') is `undefined` or @get('name') is null or @get('name') is ''
return 'Projects require a name.'
if @get('client') is `undefined` or @get('client') is null or @get('client') is ''
'Projects require a client.'
控制器:
個Rails的
class Api::ClientsController < Api::BaseController
def create
@client = Client.find_or_create_by_name(params[:client][:name])
respond_to do |format|
if @client.save
format.json { render json: @client, status: :create }
else
format.json { render json: @client.errors, status: :unprocessable_entry }
end
end
end
end
class Api::ProjectsController < Api::BaseController
def create
@project = Project.new(params[:project])
respond_to do |format|
if @project.save
format.json { render json: @project, status: :created, location: @project }
else
format.json { render json: @project.errors, status: :unprocessable_entry }
end
end
end
end
Ember.js
App.ClientsController = Em.ArrayController.extend
createClient: (data) ->
@transaction = App.store.transaction()
client = @transaction.createRecord(App.Client, data)
project_data = data.projects_attributes[0]
client.get('projects').createRecord(project_data)
validation_errors = client.validate()
if validation_errors
App.displayError validation_errors
client.destroy()
else
@transaction.commit()
App.ProjectsController = Em.ArrayController.extend
createProject: (data) ->
@transaction = App.store.transaction()
project = @transaction.createRecord(App.Project, data)
validation_errors = project.validate()
if validation_errors
App.displayError validation_errors
project.destroy()
else
@transaction.commit()
App.get('router').transitionTo('projects')
瀏覽:
灰燼。JS
App.ProjectsNewView = Em.View.extend
classNames: ['form row']
tagName: 'form'
templateName: 'projects/new'
init: ->
@_super()
submit: (event) ->
event.preventDefault()
client = {}
client.name = @get('client')
project = {}
project.name = @get('name')
client.projects_attributes = []
client.projects_attributes.push project
App.router.clientsController.createClient(client)
如果沒有人回答,可以通過在IRC頻道中粘貼Stackerflow鏈接獲得更好的成功,並在發佈鏈接後粘貼一段時間。我做了幾次,人們迴應了Stackoverflow鏈接並解決了我的問題。我知道你把它放在twitter上,這是我看到它的地方,但嘗試IRC頻道。 – brg
感謝您的建議@brg。 – David
我認爲現在可以使用RESTSerializer的embed_always映射來堅持hasMany,它也應該可以使用或不使用accept_nested_attributes_for。在github上展示它的示例應用程序的鏈接如下。 [Github上的應用程序](https://github.com/dgeb/ember_data_example)該應用程序的相關部分展示瞭如何使用單一表單保持hasMany關聯[ActiveRecord association showing has_many](https://github.com/dgeb/ ember_data_example/blob/master/app/models/contact.rb)[rails控制器,兩種模型都使用一個控制器](https://github.com/dgeb/ember_data_ – brg