在更新数据库字段中RailwayJS创建和更新
问题描述:
我是新来RailwayJS(但不是Rails)的,我有一个关于最好的方式,以节省更新我的created_at
上创建现场,并updated_at
领域的问题。在更新数据库字段中RailwayJS创建和更新
这是我的模型(DB/schema.js):
var Post = define('Post', function() {
property('title', String);
property('content', Text);
property('desc', String);
property('created_at', Date);
property('updated_at', Date);
});
所以在我posts_controller.js
,我设置create
方法之前 “created_at” 字段:
action(function create() {
req.body.Post.created_at = new Date;
Post.create(req.body.Post, function (err, post) {
// Handle error or do stuff
});
});
。 ..和我几乎在做同样的update
方法:
action(function update() {
body.Post.updated_at = new Date;
this.post.updateAttributes(body.Post, function (err) {
// Handle error or do stuff
}.bind(this));
});
不能(不应该)这是在我的模型中的过滤器之前完成的?如果是这样,怎么样?
答
正如你在最后一条评论中提到的,它可以在铁路JJ中这样完成:
before(setDate, {only: ['create', 'update']});
action(function update() {
console.log(body.Post.updated_at);
this.post.updateAttributes(body.Post, function (err) {
if (!err) {
flash('info', 'Post updated');
redirect(path_to.post(this.post));
} else {
flash('error', 'Post can not be updated');
this.title = 'Edit post details';
render('edit');
}
}.bind(this));
});
function setDate(){
body.Post.updated_at = new Date();
next();
}
答
现在,我一直是我已经张贴在我的问题...
但是,如果我想向做到这一点的before
过滤器会去是这样的:
before(setDate, {only: ['create', 'update']});
...
function setNewDate() {
// Update the request model with the date
...
next();
}
感谢您的明确示例! – JohnnyCoder 2012-11-11 16:57:19