Rails 3-如何跳过验证规则? [英] Rails 3 - how to skip validation rule?
问题描述
我有用于此验证规则的注册表格:
I have for the registration form this validation rule:
validates :email,
:presence => {:message => 'cannot be blank.'},
:allow_blank => true,
:format => {
:with => /\A[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]+\z/,
:message => 'address is not valid. Please, fix it.'
},
:uniqueness => true
此规则检查用户是否填写了注册表格的电子邮件地址(及其正确格式).
This rule check, if a user fill into the registration form email address (+ its correct format).
现在,我正在尝试增加使用Twitter登录的机会. Twitter不提供用户的电子邮件地址.
Now I am trying to add the opportunity to log in with using Twitter. Twitter doesn't provide user's email address.
在这种情况下,如何跳过上面的验证规则?
How to skip in this case the validation rule above?
推荐答案
您可以在将用户保存到代码中时跳过验证.代替使用user.save!
,而使用user.save(:validate => false)
.从 Omniauth上的轨道广播情节
You can skip validation while saving the user in your code. Instead of using user.save!
, use user.save(:validate => false)
. Learnt this trick from Railscasts episode on Omniauth
这篇关于Rails 3-如何跳过验证规则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!