2011-05-12 10 views
0

私の:userテーブルの3つの異なる部分を個別に保存するスクラッチなしのDeviseまたはAuthlogicから認証を構築しています。私はパスワードなしで、ユーザー本名と年齢確認のような非必須事項を保存するようにしています。 :user:loginnameまたは:emailのようなログインの詳細を変更したい場合は、変更を反映するために:passwordを入力する必要があります。 :user:passwordを変更したい場合も同様です。 :userテーブルのモデルは次のとおりです。パスワードを変更するとセクションが分割される問題

class User < ActiveRecord::Base 
attr_accessor :password, :old_password, :current_password 
attr_accessible :loginname, :email, :password, :password_confirmation, 
       :name, :title_forName, :age_verify, :old_password, 
       :current_password 

email_regex = /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i 
loginname_regex = /\A[\w\d]+\z/i 
password_regex = /\A(?=.*[\d])(?=.*[A-Z])([[email protected]*#$.]{6,20})\z/ 

validates :loginname, :presence  => true, 
         :format  => { :with => loginname_regex }, 
         :length  => { :maximum => 30}, 
         :uniqueness => { :case_sensitive => false } 
validates :email,  :presence  => true, 
         :format  => { :with => email_regex }, 
         :uniqueness => { :case_sensitive => false } 
validates :password, :presence  => true, 
         :on   => :create, 
         :on   => :change_password_update, 
         :on   => :change_password, 
         # :on   => :update, #This fixes the problem with the password not being validated, but breaks everything else 
         :confirmation => true, 
         :length  => { :within => 6..20 }, 
         :format  => { :with => password_regex } 
validates :name,  :length  => { :maximum => 75 } 
validates :old_password, :presence => true, 
         :on  => :change_password_update, 
         :on  => :change_password, 
         :format => { :with => password_regex } 
validates :current_password, :presence => true, 
         :on  => :change_login_update, 
         :on  => :change_login, 
         :format => { :with => password_regex } 

before_save do 
    if (!self.password.blank? || !self.password_confirmation.blank?) #This will keep from a blank password being saved to an encryption. 
     :encrypt_password 
    end 
end 

これはこれは:userログインセクション

def change_login_update 
    if @user.has_password?(params[:user][:current_password]) 
     if @user.update_attributes(params[:user]) 
     flash[:success] = "Login details updated." 
     redirect_to @user 
     else 
     @title = "Change Login Details" 
     render 'change_login' 
     end 
    else 
     flash[:notice] = "Password Didn't Match Our Records" 
     @title = "Change Login Details" 
     render 'change_login' 
    end 
    end 

のためであり、これは上の:password_changeセクション用である:user

def update 
@user = User.find(params[:id]) 
if params["change_login_update"] 
    change_login_update 
else 
    if params["change_password_update"] 
     change_password_update 
    else 
     if @user.update_attributes(params[:user]) 
     flash[:success] = "Profile updated." 
     redirect_to @user 
     else 
     @title = "Edit user" 
     render 'edit' 
     end 
    end 
end 

エンド

ためのコントローラ上の更新部であります:users co ntoller

def change_password_update 
    if @user.has_password?(params[:user][:old_password]) 
     if @user.update_attributes(params[:user]) 
     flash[:success] = "Password updated." 
     redirect_to @user 
     else 
     @title = "Change Password" 
     render 'change_password' 
     end 
    else 
     flash[:notice] = "Original Password Didn't Match Our Records" 
     @title = "Change Password" 
     render 'change_password' 
    end 
    end 

1(非重要な事柄を更新) - それは唯一の:userログインに影響を与えないものに変更されます編集ユーザーのメインページ、上のボタンを提出し、インシデントおよびすべてなしで動作します変更が必要なものは更新されます。

2(ログイン情報を更新) - submitボタンが"change_login_update"命名され、正常に動作し:email:loginnameのためのフィールド検証も同様に動作し:flash。送信をクリックすると、ユーザーには適切なパスワードが尋ねられ、データを保存する前にパスワードの一致が行われますが、:current_passwordの入力データが正しい形式であるかどうかはチェックされません。これは、:userモデルに追加されたフィールドが機能していない:onフィールドの問題であるようです。 (パスワードの更新)

は - 送信ボタンを"change_password_update":flash作品正しく命名されているが、:password:password_confirmation検証は発生しません。パスワードの一致、:old_passwordも同様に動作します。フィールドがすべて正しく入力された場合、:flash[:success]が起動しますが、パスワードは更新されません。 :on => update,を使用すると、パスワードは正しく保存されますが、:passwordは他のページで編集できないため、他のものはすべて破損します。

:onステートメントが正しいセクションで正しく発射されないという問題があるようです。これは私の初めての:onパスで働いていますので、どんな助けでも大歓迎です。前もって感謝します。

答えて

0

私のソリューションは、すべての検証が正常に働いていた。この後、検証

validates :password, :presence  => true, 
         :on   => :create, 
         :confirmation => true, 
         :length  => { :within => 6..20 }, 
         :format  => { :with => password_regex } 
validates :name,  :length  => { :maximum => 75 } 
validates :old_password, :presence => true, 
          :format => { :with => password_regex }, 
          :if  => :old_password_check? 
validates :current_password, :presence => true, 
         :if  => :login_check?, 
         :format => { :with => password_regex } 
validates :password, :presence  => true, 
         :confirmation => true, 
         :length  => { :within => 6..20 }, 
         :format  => { :with => password_regex }, 
         :if   => :old_password_check? 

:ifステートメントを追加することでしたが、パスワードが正しく保存されませんでしたし、それに対する解決策は、私が書かれていたことだった私のbefore_saveが間違ってこれがあります修正されたバージョン

before_save do 
    if(!self.password.blank?) 
     encrypt_password 
    end 
end 

すべてのものが完全に機能します。あなたのお手伝いをしていただきありがとうございます

0

私はあなたが使用できるとは思わない:上のように、あなただけ使用することができます、validates_eachと上validates_withをし、唯一のオプションは> are-:、保存:作成し、:更新

だから、あなたはカントランダムなメソッドを渡すだけです:イベントが発生したときにイベントが発生することを期待すると、保存、作成、更新時に起動できます(デフォルトは保存です)。

あなたはフィルタの前後にあなたが探していることを行うことができます。 用途:

before_create:run_before_create_validations

いずれかを作成する前に、これは、run_before_creation_validationsを起動します。 ここでは、たとえばchange_password_updateがあるかどうかを確認して、いくつかの検証を実行できます。

関連する問題