I can't figure an easy way to save a boolean value with express and mongoose. I have this schema:
var ClientSchema = new Schema({
name: {type: String, required: true, trim: true},
active: {type: Boolean }
});
var Client = mongoose.mode('Client', ClientSchema);
This is my controllers
exports.new = function(req, res) {
var client = new Client();
res.render('clients/new', { item: client } );
};
exports.create = function(req, res) {
var client = new Client(req.body);
client.save(function(err, doc) {
if (err) {
res.render('clients/new', { item: client });
}
....
});
};
And this is my view
form(method='post', action='/clients', enctype='application/x-www-form-urlencoded')
input(type='text', name='name', value=item.name)
input(type='checkbox', name='active', value=item.active)
Mongoose has the ability to map the params on req.body. On the line var client = new Client(req.body), client has the property name created from req.body with the correct value passed from the form, but the property active doesn't reflect the checkbox state.
I know that I can solve this problem adding this line after var client = new Client(req.body), but I must do it for every checkbox I add to my forms:
client.active = req.body.active == undefined ? false : true;
Question edited
I don't need to do that trick on ruby on rails. How can I use checkboxes without adding the previous line for every checkbox? This is the only way to save values from checkboxes or is there any alternative?
Edit
I have another case where the schema is defined this way
var ClientSchema = new Schema({
name: {type: String, required: true, trim: true},
active: {type: Boolean, default: true }
});
Note that active is true by default, so if I uncheck the checkbox, active it will be true, not false.