私の: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
パスで働いていますので、どんな助けでも大歓迎です。前もって感謝します。