1

Ich versuche, die Passwortbestätigung nur auf der Seite einzurichten, wo der Benutzer sein Passwort ändert. Mein Modell sieht so aus:Authlogic - Wie setze ich password_confirmation nur für update?

class User < ActiveRecord::Base 
    attr_accessor :password_confirmation 

    acts_as_authentic do |c| 
    c.validate_login_field = false 
    c.validate_password_field = false 
    c.require_password_confirmation = true 
    c.logged_in_timeout(15.minutes) 
    end 

    validates :name, :presence => {:message => 'cannot be blank.'}, :allow_blank => true, :length => {:minimum => 3, :maximum => 40}, :on => :create 
    validates :email, :presence => {:message => 'address cannot be blank.'}, :allow_blank => true, :format => {:with => /\A[A-Za-z0-9._%+-][email protected][A-Za-z0-9.-]+\.[A-Za-z]+\z/, :message => 'address is not valid. Please, fix it.'}, :uniqueness => true 
    validates :password, :presence => {:message => 'cannot be blank.'}, :allow_blank => true, :length => { :minimum => 6, :maximum => 40}, :on => :create 
    validates :password_confirmation, :presence => {:message => 'cannot be blank.'}, :allow_blank => true, :length => { :minimum => 6, :maximum => 40 }, :on => :update 
end 

und meine Methode, die neue Passwort zu speichern:.

def change_password 
    @user = current_user 
    if @user.valid_password?(params[:user][:old_password]) 
     if @user.update_attributes(params[:user].reject{|key, value| key == "old_password"}) 
     flash[:notice] = 'Your password was successfuly changed.' 
     redirect_to :back 
     else 
     flash[:warning] = 'You did not fill twice your new password correctly. Please, fix it.' 
     redirect_to :back 
     end 
    else 
     flash[:warning] = 'Your old password is WRONG! What is your malfunction!?!' 
     redirect_to :back 
    end 
    end 

Mein Problem ist, dass, wenn ich die Form das alte Passwort, dann neues Passwort (zB new_password) und dann die Bestätigungen des neuen Passworts (zB new_password1), so dass das neue Passwort & in der Datenbank gespeichert wird - aber es sollte nicht, weil die neues Passwort und die Bestätigung des neuen Passworts sind nicht das gleiche ...

Wie sollte ich die Validierungsregeln einrichten, oder wo könnte ein Problem sein?

Vielen Dank für Ratschläge

Antwort

1

Sie brauchen nur das Kennwort zu überprüfen, ob es sich geändert wird. Wenn es nicht geändert wird, sollte die Validierung für das Feld password übersprungen werden.

Railscasts.com episode #41 zeigt Ihnen, wie Sie dies tun.

Verwandte Themen