我正在运行标准geddy
设置mongo
。
路线:
router.get('/submit').to('Urls.add');
router.post('/create').to('Urls.create');
控制器:
// invokes the form for submitting a new url
// sends form output to the create function
this.add = function (req, resp, params) {
this.respond({params: params});
};
// creates a new url object and saves it to the database
this.create = function (req, resp, params) {
var self = this;
var timestamp = new Date().getTime();
var url = geddy.model.Url.create({
title: params.title,
url: params.url,
score: 0,
created: timestamp
});
url.save(function(err, data) {
if (err) {
params.errors = err;
self.transfer('add');
} else {
this.redirect({controller: 'Url.index'});
}
});
};
模型:
var Url = function () {
this.defineProperties({
title: {type:'string', required:true},
url: {type:'string', required:true},
score: {type:'int', required:true},
created: {type:'datetime', required:true}
});
this.validatesPresent('title');
this.validatesPresent('url');
};
Url = geddy.model.register('Url', Url);
看法:
<div class="row">
<form class="form-horizontal" role="form" action="/create" method="POST">
<div class="form-group">
<label for="title" class="col-lg-1 control-label">Title</label>
<div class="col-lg-7">
<input type="text" class="form-control" id="title" placeholder="title">
</div>
</div>
<div class="form-group">
<label for="url" class="col-lg-1 control-label">URL</label>
<div class="col-lg-7">
<input type="text" class="form-control" id="url" placeholder="url">
</div>
</div>
<div class="form-group">
<div class="col-lg-offset-1 col-lg-7">
<button type="submit" class="btn btn-default">Submit</button>
</div>
</div>
</form>
</div>
当我访问/submit
并填写表格时,我只是再次被重定向到同一个表格。没有任何东西被插入到数据库中。这是我第一次使用 geddy,所以我可能遗漏了一些东西。