问题描述
我正在使用devise进行身份验证,并且在设置设置之后,我已经向users表添加了一些其他字段。用户只需输入电子邮件和密码,注册用户即可编辑个人资料后即可注册。因为我已经使用:on =>更新。但现在当我尝试重置密码验证是触发错误,像名字不能是空白和blah blah。我正在使用devise并使用注册#编辑重置密码。以下是我的用户模型。
I'm using devise for authentication and I've added some other fields to the users table after setting up devise. User can sign-up by entering email and password only and after sign-up user can edit his profile. For that I've used :on => update. But now when I'm trying to reset the password validations are triggering error like name cannot be blank and blah blah. I'm using devise and using registrations#edit for resetting password. Below is my user model.
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
attr_accessible :email, :password, :password_confirmation, :remember_me, :name, :dob, :phone, :address, :state, :city, :country, :photo
has_attached_file :photo, :styles => { :small => "150x150>", :medium => "300x300>", :large => "500x500>" }
has_many :blogs, :dependent => :destroy
has_many :comments, :dependent => :destroy
has_many :followers, :through => :blogs
has_many :followings, :class_name => 'Follower', :foreign_key => 'user_id'
has_many :following_blogs, :through => :followings, :source => :blog
has_many :blog_followers, :through => :followers, :source => :user
phone_regex = /^[0-9]+$/
validates_attachment_size :photo, :less_than => 3.megabytes
validates_attachment_content_type :photo, :content_type => ['image/jpeg','image/png','image/jpg','image/gif']
validates :name, :presence => true, :on => :update
validates :dob, :presence => true, :on => :update
validates :phone, :format => { :with => phone_regex }, :length => { :is => 10 }, :on => :update
validates :address, :presence => true, :on => :update
validates :state, :presence => true,:length => { :maximum => 30 }, :on => :update
validates :city, :presence => true, :length => { :maximum => 30 }, :on => :update
validates :country, :presence => true, :length => { :maximum => 30 }, :on => :update
end
推荐答案
实际上,您正在使用可验证的设计模块。它将自动验证放在电子邮件&甚至更新密码。更好的选择是在电子邮件&密码创建并在模型中进行更改或完全删除。
Actually, the issue is you are using validatable devise module. It puts the automatic validations on email & password even on updates. The better option would be to add your own validation on email & password on create and make these changes in the model or remove it entirely.
User Model
def email_required?
false
end
def password_changed?
false
end
希望有所帮助。
这篇关于当我尝试重置密码时触发验证错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!