简体   繁体   English

在act_as_commentable gem中添加验证

[英]Add validation in acts_as_commentable gem

I'm using a gem called acts_as_commentable 我正在使用一个名为acts_as_commentable的宝石

I have added a custom column in the migration like: recipient_id 我在迁移中添加了一个自定义列,例如: recipient_id

Then I generated a Comment model as per documentation: 然后我根据文档生成了一个Comment模型:

rails g comment

Now, in my Comment.rb , I have following line: 现在,在我的Comment.rb中 ,有以下一行:

validate :comment, :recipient_id, :presence => true

Note: comment column is already added by gem itself 注意:gem本身已经添加了comment

Still, after following documentation, when I fire the following deliberately: 不过,在遵循以下文档后,当我故意触发以下内容时:

commentable = Post.create(:title => ......)
comment = commentable.comments.create
comment.comment = "Some comment"
comment.recipient_id = nil
comment.save!

The comment object seems like: 评论对象看起来像:

<Comment id: 1, comment: "Some comment", commentable_id: 1, commentable_type: "Post", recipient_id: nil, created_at: "2015-06-13 09:41:23", updated_at: "2015-06-13 09:41:23">

Why it's not validating presence of recipient_id ? 为什么没有验证的存在recipient_id

Your are calling validate instead of validates . 您正在调用validate而不是validates They both are different. 他们俩是不同的。

It should be: 它应该是:

validates :comment, :recipient_id, :presence => true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM