简体   繁体   English

如何在Rails的实例上运行validates_presence_of?

[英]How do i run validates_presence_of on an instance in rails?

I would like to do the following from a library class? 我想从库类中执行以下操作?

class LibClass
  def create_user(provider)
    user = User.new
    if provider == "facebook"
      user.validates_presence_of :email
    else
      # dont validate presence of email
    end
  end
end

I am aware that I can do a self.validates_presence_of :email inside the User class, but I am in the library class and am not sure how to do this? 我知道可以在User类中执行self.validates_presence_of:email,但是我在类库中,不确定如何执行此操作?

In your User model, you can create an accessor to toggle the validation on or off. 在您的User模型中,您可以创建访问器以打开或关闭验证。

class User < ActiveRecord::Base
  attr_accessor :validate_email

  def validate_email?; validate_email == true; end

  validates_presenece_of :email, :if => validate_email?
end

You can turn the validation on by setting user.validate_email = true : 您可以通过设置user.validate_email = true来打开验证:

def create_user(provider)
  user = User.new
  if provider == "facebook"
    user.validate_email = true
  else
    user.validate_email = false
  end
end

To actually get the validations to run, you can call user.valid? 要实际运行验证,可以调用user.valid? and the hash user.errors will be populated with any validation errors. 哈希user.errors将填充任何验证错误。

validates_presence_of is a class method, not an instance method. validates_presence_of是一个类方法,而不是实例方法。 In order to do what you want you'll have to do this: 为了执行您想要的操作,您必须执行以下操作:

user.class_eval {validates_presence_of :email }

but the validation will be set on the class, and will be on for all your users. 但验证将在课程上进行,并且将对所有用户进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Rails:如何有条件地“ validates_presence_of”? 例如到达某个页面时? - Rails: How do I “validates_presence_of” conditionally? For example when reaching a certain page? 是否已经不赞成`validates_presence_of`(从Rails 3开始)? - Is `validates_presence_of` deprecated (as of Rails 3)? 如何测试has_many:through关联,该关联在与Factory Girl in Rails的关联上也具有validates_presence_of验证? - How do I test a has_many :through association that also has a validates_presence_of validation on the association with Factory Girl in Rails? 除了在用户模型上的电子邮件和密码外,我该如何在Devise中验证新列“ validates_presence_of”? - How do I do 'validates_presence_of' new column in Devise - other than email & pw on the User model? Ruby on Rails:关于validates_presence_of的问题 - Ruby on Rails: question about validates_presence_of Rails validates_presence_of 具有多个字段 - Rails validates_presence_of with multiple fields validates_presence_of并在轨道上使用模板ruby - validates_presence_of and with templates ruby on rails 和/或Ruby on Rails模型的validates_presence_of中的运算符 - and or operator in validates_presence_of of a Ruby on Rails model Rails 3.2:validates_presence_of继承的模型 - Rails 3.2 : validates_presence_of inherited model validates_presence_of无法正常工作...如何调试? - validates_presence_of not working properly…how to debug?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM