设计密码确认错误

发布于 2024-11-30 16:48:36 字数 1266 浏览 0 评论 0原文

我正在尝试实现更改密码视图。目前,我正在测试如果我输入的密码和密码确认不匹配,是否会在表单上收到红色错误消息。我收到一条回溯信息,显示“验证失败:密码与确认不匹配”,但我没有看到任何基于自然形式的错误消息...

另一个可能有用的信息是我的用户模型接受另一个模型的嵌套属性 (contact_info)。那东西一切都很好。

这是相关的代码。从我的模型:

class User < ActiveRecord::Base
  devise :database_authenticatable, :recoverable, :rememberable, :trackable, :validatable
  ...
end

这是我的 haml 视图中的代码:

= form_for @user do |user|
  -if @user.errors.any?
  #error_explanation
    %h2= "#{pluralize(@user.errors.count, "error")} prohibited this user from being saved:"
    %ul
      - @user.errors.full_messages.each do |msg|
        %li= msg

最后,这是我的控制器中的部分:

...
if @user.update_attributes!(params[:user])
  format.html { redirect_to(users_url, :notice => 'User was successfully updated.') }
  format.xml  { head :ok }
else
  ...

我能够验证这些字段是否在传入的参数中:

Parameters: {"user"=>{"last_name"=>"Jim", "first_name"=>"Dandy", "email"=>"[email protected]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"

我怀疑我错过了一些相当简单的东西。 ..但对于我的一生,我无法弄清楚它可能是什么。

I'm trying to implement a change password view. Currently I'm testing if I get red error messages on the form if I put in password and password_confirmation that don't match. I'm getting a traceback saying "Validation failed: Password doesn't match confirmation", but I'm seeing none of the natural form based error messages...

One other tidbit of information that might be useful is that my User model accepts nested attributes for another model (contact_info). That stuff all works fine.

Here are the relevant bits of code. From my model:

class User < ActiveRecord::Base
  devise :database_authenticatable, :recoverable, :rememberable, :trackable, :validatable
  ...
end

Here is the code from my haml view:

= form_for @user do |user|
  -if @user.errors.any?
  #error_explanation
    %h2= "#{pluralize(@user.errors.count, "error")} prohibited this user from being saved:"
    %ul
      - @user.errors.full_messages.each do |msg|
        %li= msg

And finally, here is the bit from my controller:

...
if @user.update_attributes!(params[:user])
  format.html { redirect_to(users_url, :notice => 'User was successfully updated.') }
  format.xml  { head :ok }
else
  ...

I was able to verify that those fields are in the params being passed in:

Parameters: {"user"=>{"last_name"=>"Jim", "first_name"=>"Dandy", "email"=>"[email protected]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"

I suspect I'm missing something fairly simple...but for the life of me I can't figure out what it might be.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不语却知心 2024-12-07 16:48:36

所以事实证明,真正缺少的是当前密码——我只有密码和password_confirmation字段,而Devise还想要一个current_password字段。

一旦我有了这个字段,我就调用了 update_with_password 而不是 update_attributes,整个事情就很好地结合在一起了。

So it turns out that what was really missing was the current password--I only had the password and password_confirmation fields and Devise also wanted a current_password field.

once I had that field in place I called update_with_password instead of update_attributes, and the whole thing came together nicely.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文