0

I have been trying to fetch the id of this Select helper all day, and still noting but nil's and undefined... I just want the product_id to be set to the value in the Select which is being set fine in the template....

// Models

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')
});

// Route + routes

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

All the other values are being saved alright... but product_id is never set. Very frustrating when I can actually see the ID being bound to the option value in the html:

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

I hope someone can help.. Have been stuck here for waaayyy to long now :/

UnknownFrequency
  • 413
  • 3
  • 18
  • If your select is in a form, then it should have a name attribute... Otherwise, please, check your logs to see if the id is submitted to rails from the browser, and if it is... well, we'll take it from there.. – Ruby Racer Nov 03 '14 at 22:22
  • Try to put the action into the controller. Work with the attributes `content`, `optionLabelPath`, `optionValuePath` and `selectionBinding` in the select view worked very well for me. Then access the property from `selectionBinding` in the action to retrieve the id. Oh and best if you put stuff like that in a jsbin, easiest to help ;) – Preexo Nov 04 '14 at 03:50

0 Answers0