validates :tag, format: { with: /[^?]+/, message:'cannot have question marks') }
我想验证失败该值:如何从Rails验证中的属性中删除问号?
tag = "why not?"
,并通过这一个:
tag = "why not"
我不明白如何简单的Chomp的 '?'在与正则表达式的字符串。
谢谢!
validates :tag, format: { with: /[^?]+/, message:'cannot have question marks') }
我想验证失败该值:如何从Rails验证中的属性中删除问号?
tag = "why not?"
,并通过这一个:
tag = "why not"
我不明白如何简单的Chomp的 '?'在与正则表达式的字符串。
谢谢!
你可以写你的验证这样的:
validates :tag, format: { without: /\?/, message: 'cannot have question marks' }
参考:http://apidock.com/rails/ActiveModel/Validations/HelperMethods/validates_format_of
如果你想删除字符串中的所有的?
,简单DO:"why not?".gsub("?", '')
编辑 我只是重读你的问题,你想要的正则表达式的验证(啊,我需要咖啡)。
你需要躲避?
因为?
在正则表达式表示零的一...所以只要改变你的验证来 - >validates :tag, format: { with: /\?/, message:'cannot question marks') }
真棒,谢谢,我没有看到,我可以这样做:不 – user1946151 2015-02-11 23:00:45
很乐意帮忙!如果您认为这有帮助,请不要忘记将答案标记为正确:) – dgilperez 2015-02-11 23:08:16