私はgithub の Authlogic サンプル チュートリアルに従い、すべてをセットアップして実行しました。しかし、パスワードの確認に関して変更を加えたいと思います。
チュートリアルに従って、登録時にパスワード確認を入力する必要があります。私はそれを必要としないので、ブロックに入れc.require_password_confirmation = false
ました。acts_as_authentic
ただし、これによりパスワードの確認が完全に削除されます。パスワードを変更するときのために、[ユーザーの編集] ページのパスワード確認が必要です。また、パスワードのリセット ページ (現在は設定していません) にも使用したいと思います。
どうすればこれを行うことができますか?
また、それほど重要ではありませんが、[ユーザーの編集] ページでは、現在すべてが 1 つのフォームであり、1 つの更新定義がUsersController
. したがって、誰かが他の情報を変更したい場合は、私が現在設定しているため、現在のパスワードも入力する必要があります...
def update
@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] = 'Successfully updated profile.'
render :action => :edit
else
render :action => :edit
end
else
flash[:notice] = 'Your old password is wrong.'
render :action => :edit
end
end
メールアドレスを変更するか、新しいパスワードを入力する場合にのみ、古いパスワードを入力する必要があるようにしたいと思います。
user.rb
class User < ActiveRecord::Base
acts_as_authentic do |c|
c.require_password_confirmation = false
end
attr_accessor :old_password, :reset_password
validate :old_password_valid, :on => :update, :unless => [:reset_password]
def old_password_valid
errors.add(:old_password, "You must introduce your password") unless valid_password?(old_password)
end
def require_password?
password_changed? || (crypted_password.blank? && !new_record?) || reset_password
end
def deliver_password_reset_instructions!
reset_perishable_token!
Notifier.deliver_password_reset_instructions(self)
end
end