form_for验证它不起作用

时间:2012-10-28 21:30:57

标签: ruby-on-rails ruby ruby-on-rails-3.2

我有以下form_for来重置密码。

<% provide(:title, "Reiniciar Password") %>

<%= form_for(@user, :url => password_reset_path(params[:id]) ) do |f| %>
    <%= render 'shared/error_messages'%>
    <div class="row">
        <div class="span4">
            &nbsp
        </div>
        <div class="span4" id="login-box">  
            <div id="login-controls">
                <%= link_to(image_tag("logo.png"), root_path) %>
                <br>
                <br>
                    <%= f.password_field :password, :placeholder => "Contrasena", :tabindex => 1, :style => "height:25px;" %>
                    <%= f.password_field :password_confirmation, :placeholder => "Contrasena", :tabindex => 2, :style => "height:25px;" %>
                    <%= f.button "<i class=\"icon-lock icon-white\"></i> Actualizar Password".html_safe, :tabindex => 2,  class: "btn btn-warning", :style => "width:220px;margin-bottom:5px;" %>
<% end %>
          </div>
        </div>
        <div class="span4">
        </div>
    </div>

一切运行正常,验证验证,如果您在密码和密码_确认字段中输入任何类型的按钮,控制器将使用空白密码重置密码。在我的模型中,我有验证密码和密码确认,它在您创建新用户时有效,但我不知道为什么在此表单中重置密码它不起作用。

这是模型

class User < ActiveRecord::Base
  attr_accessible :email, :password, :password_confirmation
  has_secure_password

  before_save { |user| user.email = email.downcase }
  before_save :create_remember_token

  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false }
  validates :password, presence: true, length: { minimum: 6 }, confirmation: true, unless: Proc.new { |a| !a.new_record? && a.password.blank? }

    def send_password_reset
        self.password_reset_token = SecureRandom.urlsafe_base64
        self.password_reset_at = Time.zone.now
        save!
        UserMailer.password_reset(self).deliver
    end

    def reset_password_token
        self.password_reset_token = nil
        self.password_reset_at = nil
        save!
    end

  private

    def create_remember_token
        self.remember_token = SecureRandom.urlsafe_base64
    end

end

感谢您的帮助。


更新

这是控制器

class PasswordResetsController < ApplicationController

    layout "sessions"

  def new
  end

  def create
    user = User.find_by_email!(params[:password_resets][:email] )
    user.send_password_reset if user
    redirect_to root_url, :notice => "Las instrucciones para reestrablecer la contrasena fueron enviadas."
  end

  def edit
    @user = User.find_by_password_reset_token!(params[:id])
  end

  def update
    @user = User.find_by_password_reset_token!(params[:id])
    if @user.password_reset_at < (2.hours.ago).to_date
        redirect_to new_password_reset_path, :alert => "El link para actualizar la contrasena ha expirado."
    elsif @user.update_attributes(params[:user])
        @user.reset_password_token
        redirect_to root_url, :notice => "La contrasena ha sido cambiada."
    else
        render :edit
    end
  end

end

4 个答案:

答案 0 :(得分:0)

好的,我想我找到了问题的原因:

validates :password, presence: true, length: { minimum: 6 }, confirmation: true, unless: Proc.new { |a| !a.new_record? && a.password.blank? }

如果密码为空,此行会跳过验证,因此这就是为什么用户没有密码保存的原因

答案 1 :(得分:0)

我认为你的proc会在更新的情况下跳过密码验证:

validates :password, presence: true, length: { minimum: 6 }, confirmation: true, 
    unless: Proc.new { |a| !a.new_record? && a.password.blank? }
除非记录不是新的(更新)且密码为空(未在字段中提交任何内容),否则

将验证。你能尝试评论一下这个过程,看看会发生什么吗?

e.g。

validates :password, presence: true, length: { minimum: 6 }, confirmation: true 

答案 2 :(得分:0)

可能是您的Proc Block,它会检查模型是否不是新记录,密码是否为空。在这种情况下,验证不会发生。

unless: Proc.new { |a| !a.new_record? && a.password.blank? }

因此,如果您已经创建了用户:

!a.new_record? = true

并且因为密码不是直接保存的,但因为加密密码(password_digest)也将为零:

a.password.blank? = true 

因此,使用此验证编辑现有用户将不会验证密码/ password_confirm

至少我是这么认为的;)

也许您可以将其更改为:

validates :password, presence: true, length: { minimum: 6 }, confirmation: true, :if => :password?

答案 3 :(得分:0)

伙计问题出现在模型上。这是新的验证。

validates :password, presence: true, length: { minimum: 6 }, confirmation: true, unless: Proc.new { |a| !a.new_record? && a.password.blank? && a.password_reset_token.blank? }

感谢您的帮助和时间!