我有一个具有用于创建帐户的验证密码的应用程序。但是,当用户更新他的密码时,它会忽略所有验证。
用户控制器
class User < ActiveRecord::Base
attr_accessible :email, :password, :password_confirmation, :name,
attr_accessor :password
before_save :encrypt_password
before_save { |user| user.email = email.downcase }
before_save { |user| user.name = name.downcase }
before_create { generate_token(:auth_token) }
before_create { generate_token(:email_token) }
VALID_PASSWORD_REGEX = /^(?=.*[a-zA-Z])(?=.*[0-9]).{6,}$/
validates_confirmation_of :password
validates :password, :on => :create, presence: true, format: { with: VALID_PASSWORD_REGEX }
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false }
validates :name, presence: true, format: { with: VALID_NAME_REGEX }, uniqueness: { case_sensitive: false }, length: { minimum: 4 }
所以我试图删除:on => :create,
,但是当我登录时,我收到一个错误,提示密码无效,即使它是。所以它不允许@user.save!
用户控制器
def update
if @user.update_attributes(params[:user])
redirect_to @user
else
render "edit"
end
end