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