无法创建新记录
问题描述:
我一直在努力使这项工作无数小时。我无法找到我可以在网络上理解的答案。像我这样的初学者其他可能有,我有,所以我张贴我的问题同一个问题:无法创建新记录
我有我的灰烬应用此错误: 未捕获的错误:断言失败:您只能添加一个'用户'记录到这种关系
我试图创建一个“Cond”的新记录。但是我无法将'user'对象添加到新创建的Cond实例的用户属性中。但是,从Chrome调试工具中,我可以看到“用户”不是null或未定义。这里是控制器崩溃:
App.BookController = Ember.Controller.extend({
needs: 'user',
user: Ember.computed.alias("controllers.user"),
frequency: ['Every Minute', 'Every Hour', 'Every Day', 'Every Week', 'Every Month', 'Every Quarter', 'Every Year', 'Continuous'],
selectedFrequency: 'Every Month',
actions: {
createCond: function() {
var user = this.get('user');
var Cond = this.store.createRecord('Cond', {
name: this.get('name'),
frequency: this.get('selectedFrequency'),
description: this.get('description'),
createdDate: new Date(),
});
Cond.set('user', user); // Application crash her
user.addObject('Conds', Cond);
Cond.save().then(function() {
user.save();
console.log('new Cond created');
});
}
}
});
感谢您的帮助!
答
您抓住用户控制器,模型是控制器上的一个属性。
要么改变别名:
user: Ember.computed.alias("controllers.user.model"),
或您的GET更改为:
var user = this.get('user.model');
这是伟大的工作。感谢Kingpin2k!为了其他用户的利益,请注意我的代码中有另一个错误:'user.addObject('Conds',Cond);'应该改为:user.get('conds')。pushObject(cond); – 2014-10-18 14:39:17