I am trying to populate a dropdown list for a form for one of my models (cars), with data from another model (colours), but cannot seem to figure out how to do this. I need to somehow call the list of colours into the dropdown in the cars.jade file, so that when a user selects the auto-filled list I need the ObjectID to be the value of the item (which subsequently is saved as a new car.
Cars New Form (car.jade):
/* This is the bit I'm having trouble with */
select(name='carColours')
option(value='')
each colour in colours
- var select=null; if (colour.title == carColours.title) select='selected';
option(value=colour._id, selected=select)= colour.title
Cars controller (cars.js):
exports.new = function(req, res){
res.render('cars/new', {
title: 'New Car',
event: new Car({})
})
}
Cars model (car.js):
var CarSchema = new Schema({
title: {type : String, default : '', trim : true},
colour: {type : Schema.ObjectId, ref : 'Colour'},
})
Colours model (colour.js)
var ColourSchema = new Schema({
title: {type : String, default : '', trim : true},
hexadecimal: {type : String, default : '', trim : true},
})
ColourSchema.statics = {
list: function (options, cb) {
var criteria = options.criteria || {}
this.find(criteria)
.sort({'title': 1}) // sort alphabetically
.exec(cb)
}
}
coloursarray in care.jade coming from? And what error are you getting exactly, if any?