3

パスワードをリセットするための次の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>

検証を除いて、すべてが完璧に機能します。passwordとpassword_confirmationフィールドに何も入力せずにボタンを押すと、コントローラーは空白のパスワードでパスワードをリセットします。私のモデルでは、パスワードとパスワード確認の検証があり、新しいユーザーを作成するときに機能しますが、このフォームでパスワードをリセットしても機能しない理由がわかりません。

これがモデルです

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

4 に答える 4

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? }

あなたの助けとあなたの時間をありがとう!

于 2012-10-29T00:16:16.737 に答える
0

わかりました、私はあなたの問題の原因を見つけたと思います:

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

パスワードが空白の場合、この行は検証をスキップするため、パスワードなしでユーザーが保存されます

于 2012-10-28T22:05:01.460 に答える
0

更新の場合、プロシージャはパスワードの検証をスキップすると思います:

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

レコードが新規 (更新) でなく、パスワードが空白 (フィールドに何も送信していない) でない限り、検証されます。proc をコメントアウトして、何が起こるかを確認できますか?

例えば

validates :password, presence: true, length: { minimum: 6 }, confirmation: true 
于 2012-10-28T22:06:49.983 に答える
0

モデルが新しいレコードではないかどうか、およびパスワードが空であるかどうかを確認するのは、Proc Block である可能性があります。この場合、検証は行われません。

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

したがって、すでに作成されたユーザーがある場合:

!a.new_record? = true

また、パスワードは直接保存されるのではなく、暗号化されたパスワード (password_digest) としても保存されるため、次のように nil になります。

a.password.blank? = true 

したがって、この検証を使用して既存のユーザーを編集しても、パスワード / password_confirm は検証されません。

少なくとも私はそう思う ;)

たぶん、次のように変更できます。

validates :password, presence: true, length: { minimum: 6 }, confirmation: true, :if => :password?
于 2012-10-28T22:11:11.100 に答える