在模型字段上实现停用词的简单方法
我正在使用 Rails 3.0,并且有几个模型,它们具有公共的、英语的参考字段,例如故事章节的标题。我正在寻找一种优雅的方法来实现控制器上操作的某些停止词,例如“新”,“更新”,“销毁”等。最好的方法是什么,在保存期间通过某种回调型号?
I am using Rails 3.0 and have several models which have a public, English, reference field such as a title on a chapter of a story. I am looking for an elegant way to implement certain stop words for actions on controllers, e.g. "new", "update", "destroy", etc. What is the best way to do this, via some kind of callback during the saving of the model?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Rails 有大量针对此类事情的回调。我认为您最感兴趣的回调是 before_validation、before_validation_on_create、after_validation 和 after_validation_on_create,因为您可以删除停用词,然后对剩下的内容运行验证。
所以你的 Chapter 模型会有类似这样的内容:
如果你需要的话,通常的 before_destroy 和 before_update 也在那里。
Rails has a tonne of callbacks for exactly this kind of thing. I think the callbacks that would be of most interest to you would be before_validation, before_validation_on_create, after_validation and after_validation_on_create since you can get in a strip out stopwords and then have your validations run on whats left.
So your Chapter model would have something like:
The usual before_destroy and before_update are also there if you need them.