gpt4 book ai didi

ruby-on-rails-3 - 验证器,密码确认

转载 作者:行者123 更新时间:2023-12-03 13:22:17 25 4
gpt4 key购买 nike

我不明白为什么模型不检查密码确认,这是模型的代码:

class User < ActiveRecord::Base
attr_accessor :password_confirmation
validates :email, :presence =>true,
:uniqueness=>true
validates :password, :presence =>true,
:length => { :minimum => 5, :maximum => 40 },
:confirmation =>true
validates_confirmation_of :password
end

Controller 旨在从 View 中获取数据并尝试执行保存,这是 View 的代码:
<h1>Registration process</h1>
<%= form_for(@new_user) do |f|%>
<% if @new_user.errors.any? %>
<div id="errorExplanation">
<h2><%= pluralize(@new_user.errors.count, "error") %> prohibited this article from being saved:</h2>
<ul>
<% @new_user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.label :email %><br />
<%= f.text_field :email %><br />
<%= f.label :password %><br />
<%= f.password_field :password %><br />
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %>
<%#TODO Confirm password%>

<%= f.submit 'Join' %>
<%end%>

如果密码不匹配,则不会出现错误。

最佳答案

我也刚刚被这个烫伤了。我怀疑你的确认值是 nil .从文档:

NOTE: This check is performed only if password_confirmation is not nil, and by default only on save. To require confirmation, make sure to add a presence check for the confirmation attribute:


  • Ruby on Rails API: validates_confirmation_of

  • 此外,您不需要 attr_accessor :password_confirmation ,因为验证会为您添加它。导轨!

    关于ruby-on-rails-3 - 验证器,密码确认,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4697536/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com