从“选择”菜单中获取ID并将记录保存为一对多

时间:2014-11-03 21:46:24

标签: ruby-on-rails ember.js ember-data ember-rails

我一直在努力获取这个Select帮助程序的ID,但仍然注意到nil&unde; ... 我只想将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})
});

/consumption/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 />
...

// controller

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 Serializers

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。当我真正看到ID绑定到html中的选项值时非常令人沮丧:

<select id="ember983" class="ember-view ember-select">
  <option id="ember996" class="ember-view" value="1">A-Product</option>

我希望有人可以提供帮助..现在已经被困在waaayyy很久了:/

0 个答案:

没有答案