在此处输入代码我使用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
// 修改 class Admin :: AccountsController&lt; 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
答案 0 :(得分:0)
在encrypt_password
方法中添加条件
def encrypt_password
if password.present? and !password.blank?
self.password = Digest::MD5.hexdigest(password)
end
end