2015-02-11 64 views
1
validates :tag, format: { with: /[^?]+/, message:'cannot have question marks') } 

我想验证失败该值:如何从Rails验证中的属性中删除问号?

tag = "why not?" 

,并通过这一个:

tag = "why not" 

我不明白如何简单的Chomp的 '?'在与正则表达式的字符串。

谢谢!

回答

0

如果你想删除字符串中的所有?,简单DO:"why not?".gsub("?", '')

编辑 我只是重读你的问题,你想要的正则表达式的验证(啊,我需要咖啡)。

你需要躲避?因为?在正则表达式表示零的一...所以只要改变你的验证来 - >validates :tag, format: { with: /\?/, message:'cannot question marks') }