It's a classic case of create and edit a form data. I got a complex form that suppose to render back all fields to validate it, but doesn't works. For this case, I've created a simple example to illustrate my concern and try to understand what's happing.
In this sample app body parser and method override are up:
app.use(express.bodyParser());
app.use(express.methodOverride());
Now I have a simple form in user.jade:
doctype 5
html
header
title Add new user
body
h4 User - Add
form(id='userForm', method='post', action='/user/save')
label(for='name') Name
input(type='text', id='name', name='name')
label(for='address') Address
input(type='text', id='address', name='address')
input(type='submit', value='Save')
The GET and POST HTTP Methods are configured:
app.get('/user/new', user.create); //create new user
app.post('/user/save', user.save); //save or edit user in validation process
Finally the "create" and "edit/save" functions:
exports.create = function(req, res) {
res.render('user');
};
exports.save = function(req, res) {
var name = req.body.name;
var address = req.body.address;
//do some validation
//...
//return it back to page to fix wrong fields
res.render('user', { name: name, address: address });
};
Why I render user.jade back, so the fields are not filled? Can't use res.render to fill page form? Thanks in advance!
In user.jade you aren't using #{name} or #{address}
try something like this to access the local variable
input(type='text', id='name', name='name', value=#{name})