ruby on rails - How to encrypt password only if it is not empty -


Enter the code here I am using MD5 to encrypt my password, though The user does not edit his password. That's why except for password but the password is always updated in my implementation.

  The code in my account is model. Rb square administrator :: account  

// < Strong> edit class admin :: accounting controller & lt; ApplicationController

  before_filter: check_login, only: [: new,: index, edit: show, delete] before_action: set_admin_locale, only: [: new,: index, edit , Show: delete, create, update: layout 'admin_layout' # first_one: set_admin_account, only: [: show,: edit, update: deleted] #GET / admin / accounts #GET / admin / accounts Json def Update_user @admin_account = Admin :: Account.find (params [: id]) response_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-to-end personal action to share common setup or barriers between Use callback Def set_admin_account @admin_account = Admin :: Account.find (params [: id]) End # Do not trust criteria from scary internet, only allow white list through para [[admin_account] [: password] ! = "" Params.require (: admin_account) [admin_account_params gender = "m" if the consultation [: admin_account] [: gender] == "female" gender = "f" end if the consultation [: admin_account] [: password]! Permit (: email, password :: name, account_type, student_number,: nationality, photo :: date_of_birth). Merge (: gender = & gt; gender) Other parameters.Require (: admin_account) .permit (: email,: name, account_type, student_number,: nationality, photo :: date_of_birth) .merge (: gender = & gt; Gender) end end def register_account gender = "m" if paramount [: admin_account] [: gender] == "woman" gender = "f" end if the consultation [: 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 = & gt; Student ', gender = & gt; gender) Other Params.Require (: admin_account) .permit (: gender, email: password: name, password_confirmation, nationality, photo: : Date_of_birth). Merge (: account_type = & gt; student no ', gender = & gt; gender) end of end  

Change your encrypt_password method

  def encrypt_password if password.present? And! Password.blank? Self.password = Digest :: MD 5.hexDest (password) end and  

Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -