mysql - How to Encrypt data after validation in Ruby on Rails? -
i using devise authentication, register. want save emailid in mysql in encrypted format. use gem 'aescrypt'.
my controller:
def create @dashboard_user = dashboarduser.new(dashboard_user_params) @dashboard_user.created_by=current_dashboard_user.username @dashboard_user.company_id=current_dashboard_user.company_id active_ind = "" email = @dashboard_user.email if params["active"] == nil active_ind = "0" else active_ind = "1" end @dashboard_user.active = active_ind @dashboard_user.email= aescrypt.encrypt(email, "password") respond_to |format| if @dashboard_user.save format.html { flash[:notice] = 'user created.' , redirect_to action: "index"} else @dashboard_user.email = email format.html { render :new } end end end
when try save user, throws email invalid. removed validation email in model. though same error exists. problem is? if there way encrypt data after validation?
thanks in advance.
how validate email? use custom method decrypt before validate against (for e.g.) regex. alternatively, can use activerecord callbacks. in case, after_validation
can useful :)
after_validation(on: :create) self.email= aescrypt.encrypt(email, "password") end
Comments
Post a Comment