如何在Rails中进行异常验证

时间:2021-10-16 11:40:58

I'm a newbie in rails and I'm stuck with this problem: I have a model named User

我是rails的新手,我遇到了这个问题:我有一个名为User的模型

class User < ActiveRecord::Base
  attr_accessor :password
  EMAIL_REGEX = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i
  validates :first_name, :presence => true,:format => /\A[a-zA-Z]+\z/
  validates :last_name, :presence => true,:format => /\A[a-zA-Z]+\z/
  validates :email, :presence => true, :uniqueness => true, :format => EMAIL_REGEX
  validates :password, :presence => true
  validates_length_of :password, :in => 6..20, :on => :create
end

with database attributes first_name, last_name, email, hashed_password and encrypted_password.

数据库属性为first_name,last_name,email,hashed_pa​​ssword和encrypted_pa​​ssword。

When I create new Object of User and saves it in the database there is no problem. NOW, here's the problem I want to edit attributes of my User Object EXCEPT email and password.

当我创建新的User of User并将其保存在数据库中时没有问题。现在,这是我想编辑我的用户对象EXCEPT电子邮件和密码的属性的问题。

Once I try to edit the record through edit of rails resource it flags an error that password should not be empty. I am planning to have an exemption of validation if the user wants to edit his/her information but I know that it is not a good practice.

一旦我尝试通过编辑rails资源来编辑记录,它就会标记一个错误,即密码不应为空。如果用户想要编辑他/她的信息,我打算免除验证,但我知道这不是一个好习惯。

Hoping to find the best answer.

希望找到最好的答案。

3 个解决方案

#1


0  

For starters check out http://guides.rubyonrails.org/active_record_validations.html#conditional-validation - once this approach becomes unDRY (repeated more then 2-3 times) see http://apidock.com/rails/Object/with_options

对于初学者,请查看http://guides.rubyonrails.org/active_record_validations.html#conditional-validation - 一旦此方法变为unDRY(重复超过2-3次),请参阅http://apidock.com/rails/Object/with_options

Once you get more advanced you will want to try something in the lines of a form/service class and extract form/action-specific validations there - http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/

一旦你变得更高级,你将需要尝试表单/服务类的行,并在那里提取表单/操作特定的验证 - http://blog.codeclimate.com/blog/2012/10/17/7-方法对分解脂肪-的ActiveRecord的模型/

...and welcome to rails :)

...欢迎来到铁路:)

#2


0  

Try using

尝试使用

validates :password, :presence => true, on: :create

And in the user edit form add every field except email and password.

并在用户编辑表单中添加除电子邮件和密码之外的所有字段。

But thing is that while you are going to change the password, there is no validation on password. So when you are implementing the change_password section, you need handle it and nee to add the errors manually for the password.

但事实是,当您要更改密码时,密码无法验证。因此,当您实现change_password部分时,您需要处理它并且需要手动为密码添加错误。

#3


0  

Problem Solved! Im not sure if this is the best solution. Once I instantiate a User object I use the after_initialize then set updatePassword to true as a default value then in the edit action of User Object I set updatePassword to false so that it will be exempted in the validation. I also use the conditional validation for the updatePassword. :D Thanks for all the ideas!

问题解决了!我不确定这是否是最好的解决方案。一旦我实例化User对象,我使用after_initialize,然后将updatePassword设置为true作为默认值,然后在User Object的编辑操作中,我将updatePassword设置为false,以便在验证中免除它。我还使用updatePassword的条件验证。 :D感谢所有的想法!

#1


0  

For starters check out http://guides.rubyonrails.org/active_record_validations.html#conditional-validation - once this approach becomes unDRY (repeated more then 2-3 times) see http://apidock.com/rails/Object/with_options

对于初学者,请查看http://guides.rubyonrails.org/active_record_validations.html#conditional-validation - 一旦此方法变为unDRY(重复超过2-3次),请参阅http://apidock.com/rails/Object/with_options

Once you get more advanced you will want to try something in the lines of a form/service class and extract form/action-specific validations there - http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/

一旦你变得更高级,你将需要尝试表单/服务类的行,并在那里提取表单/操作特定的验证 - http://blog.codeclimate.com/blog/2012/10/17/7-方法对分解脂肪-的ActiveRecord的模型/

...and welcome to rails :)

...欢迎来到铁路:)

#2


0  

Try using

尝试使用

validates :password, :presence => true, on: :create

And in the user edit form add every field except email and password.

并在用户编辑表单中添加除电子邮件和密码之外的所有字段。

But thing is that while you are going to change the password, there is no validation on password. So when you are implementing the change_password section, you need handle it and nee to add the errors manually for the password.

但事实是,当您要更改密码时,密码无法验证。因此,当您实现change_password部分时,您需要处理它并且需要手动为密码添加错误。

#3


0  

Problem Solved! Im not sure if this is the best solution. Once I instantiate a User object I use the after_initialize then set updatePassword to true as a default value then in the edit action of User Object I set updatePassword to false so that it will be exempted in the validation. I also use the conditional validation for the updatePassword. :D Thanks for all the ideas!

问题解决了!我不确定这是否是最好的解决方案。一旦我实例化User对象,我使用after_initialize,然后将updatePassword设置为true作为默认值,然后在User Object的编辑操作中,我将updatePassword设置为false,以便在验证中免除它。我还使用updatePassword的条件验证。 :D感谢所有的想法!