Ruby on rails 重置密码验证错误:密码太短错误,即使密码在范围内
最近,我将密码验证代码更改为下面的代码,以便创建一个不需要密码和密码确认的表单 请注意,我没有用户的安全密码来生成我的加密密码。我在RailsTutorial中使用了SHA2.hexdigest per Hartl user.rbRuby on rails 重置密码验证错误:密码太短错误,即使密码在范围内,ruby-on-rails,ruby-on-rails-3,Ruby On Rails,Ruby On Rails 3,最近,我将密码验证代码更改为下面的代码,以便创建一个不需要密码和密码确认的表单 请注意,我没有用户的安全密码来生成我的加密密码。我在RailsTutorial中使用了SHA2.hexdigest per Hartl user.rb before_save :encrypt_password, :unless => Proc.new { |u| u.password.blank? } validates_presence_of :password, :if => :should_val
before_save :encrypt_password, :unless => Proc.new { |u| u.password.blank? }
validates_presence_of :password, :if => :should_validate_password?
validates_confirmation_of :password, :if => :should_validate_password?
validates_length_of :password, :minimum => 6, :maximum => 40, :allow_blank => true
#changing :allow_blank from false to true above fixed my problem.
def should_validate_password?
updating_password || new_record?
end
我在“记住我/重置密码”上遵循了此Railscast,并且不断出现以下错误:
ActiveRecord::RecordInvalid in PasswordResetsController\create
验证失败:密码太短(至少6个字符)
当我尝试为现有用户生成身份验证令牌时,以及再次尝试提交电子邮件以获取重置链接时,都会发生这种情况。我可以通过注释掉这段代码暂时删除这个验证,一切都很好
我尝试重新登录到用户帐户以更改密码,使其在范围内(9个字符长),但仍然收到相同长度的验证错误消息
有什么建议吗
我不知道问题是从哪里来的。我不明白为什么要验证我的密码
以下是我在密码重置控制器中的创建操作:
def create
user = User.find_by_email(params[:email])
user.send_password_reset if user
redirect_to root_path, :notice => "Email sent with password reset"
end
这是我的密码重置表:
<%= form_tag password_resets_path, :method => :post do %>
<div class="field">
<%= label_tag :email %>
<%= text_field_tag :email, params[:email] %>
</div>
<div class="actions"><%= submit_tag "Reset Password" %></div>
<% end %>
:post do%>
如果你们需要其他文件,请告诉我
编辑:使用工作解决方案更改代码
谢谢。出现错误是因为您验证了密码确认。这意味着它也将期望一个在该范围内的值 您可以通过执行以下操作来解决此问题:
validates :password, length: { minimum: 8, allow_nil: true },
confirmation: true
你不需要:应该验证密码吗 我刚刚添加了一个Proc,如下所示:
validates :password, length: { minimum: 6 }, unless: Proc.new { |user| user.password.nil? }
validates :password_confirmation, presence: true, unless: Proc.new { |user| user.password.nil? }
或者您可以使用和_选项将其分组
请参阅:解决了它<代码>:allow_blank=>true
将使此操作正常。