ruby on rails - How to encrypt password only if it is not empty -
ruby on rails - How to encrypt password only if it is not empty -
enter code here using md5 encrypting password, however, in editing user profile user tend not edit password. leaving password is. in implementation password updated.
here code in business relationship model.rb class admin::account < activerecord::base require 'digest/md5' acts_as_reader before_save :encrypt_password def encrypt_password self.password = digest::md5.hexdigest(password) end end
//edit class admin::accountscontroller < applicationcontroller
before_filter :check_login, only: [:new, :index, :edit, :show, :destroy] before_action :set_admin_locale, only: [:new, :index, :edit, :show, :destroy,:create,:update] layout 'admin_layout' # before_action :set_admin_account, only: [:show, :edit, :update, :destroy] # /admin/accounts # /admin/accounts.json def update_user @admin_account = admin::account.find(params[:id]) respond_to |format| if @admin_account.update(admin_account_params) @success = true session[:nationality] = @admin_account.nationality format.js else @success = false format.js end end end private # utilize callbacks share mutual setup or constraints between actions. def set_admin_account @admin_account = admin::account.find(params[:id]) end # never trust parameters scary internet, allow white list through. def admin_account_params gender = "m" if params[:admin_account][:gender] == "female" gender = "f" end if params[:admin_account][:password] != "" params.require(:admin_account).permit(:email, :password, :name, :account_type, :student_number, :nationality,:photo,:date_of_birth).merge(:gender => gender) else params.require(:admin_account).permit(:email, :name, :account_type, :student_number, :nationality,:photo,:date_of_birth).merge(:gender => gender) end end def register_account gender = "m" if params[:admin_account][:gender] == "female" gender = "f" end if params[:admin_account][:student_number].present? params.require(:admin_account).permit(:email, :password, :name, :password_confirmation, :student_number,:nationality,:photo,:date_of_birth).merge(:account_type => 'student', :gender => gender) else params.require(:admin_account).permit(:gender,:email, :password, :name, :password_confirmation,:nationality,:photo,:date_of_birth).merge(:account_type => 'not student',:gender => gender) end end end
put status in encrypt_password
method
def encrypt_password if password.present? , !password.blank? self.password = digest::md5.hexdigest(password) end end
ruby-on-rails encryption
Comments
Post a Comment