0

私は RailwayJS (Rails ではありません) を初めて使用し、Create でフィールドを保存し、Update でフィールドを保存する最良の方法について質問がありました。created_atupdated_at

これは私のモデルです (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の前に「created_at」フィールドを設定しています。create

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));
});

これは、モデルの before フィルターで実行できませんでしたか (実行すべきではありませんでしたか?) もしそうなら、どのように?

4

2 に答える 2

3

As you have mentioned in the last comment it can be done in railwayJS like this:

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();
}
于 2012-11-06T08:22:38.920 に答える
0

今のところ、私は自分の質問に投稿したものを保持しています...

しかし、フィルターでこれを行いたい場合は、次のようになります。before

before(setDate, {only: ['create', 'update']});

...

function setNewDate() {
  // Update the request model with the date
  ...
  next();
}
于 2012-10-12T14:23:04.997 に答える