Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/53.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 需要当前密码才能设置新密码_Ruby On Rails_Ruby_Validation_Ruby On Rails 4_Grails Validation - Fatal编程技术网

Ruby on rails 需要当前密码才能设置新密码

Ruby on rails 需要当前密码才能设置新密码,ruby-on-rails,ruby,validation,ruby-on-rails-4,grails-validation,Ruby On Rails,Ruby,Validation,Ruby On Rails 4,Grails Validation,我希望用户在编辑个人资料时输入当前密码 这就是我的用户模型的外观: attr_accessor :current_password def current_password errors[:current_password] << 'Incorrect password' unless self.current_password == self.password end validate :current_password, on: :update 和用户表单部分: <

我希望用户在编辑个人资料时输入当前密码

这就是我的用户模型的外观:

attr_accessor :current_password

def current_password
  errors[:current_password] << 'Incorrect password' unless self.current_password == self.password
end

validate :current_password, on: :update
和用户表单部分:

<div class="form-group">
  <%= f.label :password, "Current password" %>
  <%= f.password_field :current_password, class: "form-control", placeholder: "Current password" %>
</div>

但我得到的堆栈级别太深,它进入了一个验证循环

我做错了什么?

这种方法

def current_password
  errors[:current_password] << 'Incorrect password' unless self.current_password == self.password
end
所以,在这里,我不讨论attr_访问器方法:它们是简单的读写实例变量方法。验证具有不同的方法名称,因此它不会与访问器方法名称发生冲突。

此方法

def current_password
  errors[:current_password] << 'Incorrect password' unless self.current_password == self.password
end

所以,在这里,我不讨论attr_访问器方法:它们是简单的读写实例变量方法。验证具有不同的方法名称,因此它不会与访问器方法名称发生冲突。

当前\u密码正在调用自身。你有一个无限递归

你基本上是这样做的:

def current_password
  self.current_password
end

当前密码正在调用自身。你有一个无限递归

你基本上是这样做的:

def current_password
  self.current_password
end

正如其他答案所说,在当前密码中有一个无限循环。我将把验证逻辑拉到它自己的方法中。类似下面的内容

validate :has_correct_current_password, on: :update

def has_correct_current_password
  errors[:current_password] << 'Incorrect password' unless current_password == password
end
validate:on::update上是否有正确的当前密码
def拥有正确的当前密码

错误[:current_password],因为其他人回答说当前_password中有一个无限循环。我将把验证逻辑拉到它自己的方法中。类似下面的内容

validate :has_correct_current_password, on: :update

def has_correct_current_password
  errors[:current_password] << 'Incorrect password' unless current_password == password
end
validate:on::update上是否有正确的当前密码
def拥有正确的当前密码

错误[:current_password]谢谢,noob错误。虽然逻辑不正确,但在on::update验证上。self.current_password期望与用户想要设置的新密码相等,因此它总是会失败。这是真的-如果配置文件编辑包括更改密码,这将不起作用。您需要考虑如何处理该问题。您可以将新密码放入访问者中,然后进行“保存前”回调,将其写入密码字段。谢谢,没有错误。尽管逻辑不正确,但在on::update验证中。self.current_password期望与用户想要设置的新密码相等,因此它总是会失败。这是真的-如果配置文件编辑包括更改密码,这将不起作用。您需要考虑如何处理这个问题。您可以将新密码放入访问者中,然后在保存前进行回调,将其写入密码字段。