Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Updating users

  • Loading branch information...
commit 14a22d9033dcaefe69ad90e21609ed7d17360c16 1 parent 7ded5a2
@tj tj authored
View
2  examples/route-separation/app.js
@@ -16,6 +16,7 @@ var express = require('express')
app.set('view engine', 'ejs');
app.set('views', __dirname + '/views');
app.use(express.bodyDecoder());
+app.use(express.methodOverride());
app.use(express.staticProvider(__dirname + '/public'));
// General
@@ -29,6 +30,7 @@ app.all('/user/:id/:op?', user.load);
app.get('/user/:id', user.view);
app.get('/user/:id/view', user.view);
app.get('/user/:id/edit', user.edit);
+app.put('/user/:id/edit', user.update);
app.listen(3000);
console.log('Express app started on port 3000');
View
9 examples/route-separation/user.js
@@ -41,4 +41,13 @@ exports.edit = function(req, res){
, user: req.user
}
});
+};
+
+exports.update = function(req, res){
+ // Normally you would handle all kinds of
+ // validation and save back to the db
+ var user = req.body.user;
+ req.user.name = user.name;
+ req.user.email = user.email;
+ res.redirect('back');
};
View
4 examples/route-separation/views/user/edit.ejs
@@ -1,7 +1,9 @@
<div id="user">
<h1>Editing <%= user.name %></h1>
<form method="post">
- <p>Email: <input type="text" value="<%= user.email %>" name="user[name]"/></p>
+ <input type="hidden" value="put" name="_method" />
+ <p>Name: <input type="text" value="<%= user.name %>" name="user[name]"/></p>
+ <p>Email: <input type="text" value="<%= user.email %>" name="user[email]"/></p>
<p><input type="submit" value="Save" /></p>
</form>
</div>

0 comments on commit 14a22d9

Please sign in to comment.
Something went wrong with that request. Please try again.