0
在此處輸入代碼我正在使用MD5來加密我的密碼,但是,在編輯用戶配置文件時,用戶傾向於不編輯其密碼。所以保持原樣。但在我的實現中,密碼始終更新。如何僅在密碼不爲空的情況下對密碼進行加密
Here is the code in my account 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
// 編輯 類聯繫:: 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]
# GET /admin/accounts
# GET /admin/accounts.json
def update_user
@admin_account = Admin::Account.find(params[:id])
respond_to do |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
# Use callbacks to share common setup or constraints between actions.
def set_admin_account
@admin_account = Admin::Account.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the 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
對不起,不說明白了。我試過你的實現,如果我的表中的密碼列是空的,它會工作。但我希望模型檢查用戶是否在我的表單上輸入了密碼,如果他/她沒有輸入,則繞過加密。 – user3122063 2014-10-28 06:46:17
發佈您的accounts_controller也將幫助我理解。 – AshokGK 2014-10-28 07:04:33
我已經使用提供的accounts_controller編輯我的問題 – user3122063 2014-10-28 07:26:52