我整天都在尝试获取此 Select 助手的 id,但仍然注意到 nil 和未定义...我只想将 product_id 设置为 Select 中的值,该值在模板中设置得很好... .
// 楷模
Amber.Consumption = DS.Model.extend({
product: DS.belongsTo('product', {async: true}),
quantityUsed: DS.attr('number'),
location: DS.attr('string'),
employeeName: DS.attr('string'),
processed: DS.attr('boolean', {defaultValue: false})
});
Amber.Product = DS.Model.extend({
quantityStock: DS.attr('number'),
product: DS.attr('string'),
consumptions: DS.hasMany('consumption', {async:true})
});
/消费/new.hbs
<form {{action "create" on="submit"}}>
<div>
<label>Produkt<br />
{{view "select"
content=products
selection=products
optionValuePath="content.id"
optionLabelPath="content.product"
}}
</label>
</div><br />
...
// 控制器
Amber.ConsumptionsNewController = Ember.ObjectController.extend ({
products: function() {
return this.store.find('product')
}.property('product')
});
// 路线 + 路线
Amber.Router.map(function() {
this.resource('products', function() {
this.route('new');
this.route('update', { path: '/update/:product_id' });
this.route('show', { path: '/show/:product_id' });
this.resource('consumptions', function() {
this.route('new');
this.route('show', { path: '/show/:consumption_id' });
})
});
});
Amber.ConsumptionsNewRoute = Ember.Route.extend({
model: function() {
return this.store.createRecord('consumption', {
processed: true,
});
},
actions: {
create: function() {
var newConsumption = this.get('currentModel');
var self = this;
newConsumption.save().then(
function() { self.transitionTo('consumptions') },
function() { }
);
}
}
});
// Rails 序列化器
class ConsumptionSerializer < ActiveModel::Serializer
attributes :id, :product_id, :quantity_used, :employee_name, :processed, :location
end
class ProductSerializer < ActiveModel::Serializer
attributes :id, :quantity_stock, :product
end
所有其他值都被保存好了......但 product_id 从未设置。当我实际上可以看到绑定到 html 中的选项值的 ID 时,非常令人沮丧:
<select id="ember983" class="ember-view ember-select">
<option id="ember996" class="ember-view" value="1">A-Product</option>
我希望有人可以提供帮助.. 已经被困在这里很久了:/