我正在尝试设置我的程序,以便仅在更改密码时验证密码(因此用户可以编辑其他信息而无需输入密码)。
我目前收到一个错误提示
NoMethodError in UsersController#create, undefined method `password_changed?' for #<User:0x00000100d1d7a0>
当我尝试登录时。
这是我在
user.rb
中的验证码: validates :name, :presence => true,
:length => { :maximum => 50 }
validates :email, :presence => true,
:format => { :with => email_regex },
:uniqueness => { :case_sensitive => false }
validates :password, :presence =>true, :confirmation => true, :length => { :within => 6..40 }, :if=>:password_changed?
这是我在
users_controller.rb
中的创建方法:def create
@user = User.new(params[:user])
if @user.save
sign_in @user
flash[:success] = "Welcome to the Sample App!"
redirect_to @user
else
@title = "Sign up"
render 'new'
end
end
谢谢!
最佳答案
用。。。来代替:
:if => lambda {|user| user.password_changed? }
我会做两种不同的验证:
validates :password, :presence =>true, :confirmation => true, :length => { :within => 6..40 }, :on => :create
validates :password, :confirmation => true, :length => { :within => 6..40 }, :on => :update, :unless => lambda{ |user| user.password.blank? }
关于ruby-on-rails - 未定义的方法 password_changed?错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7080892/