Rails 4: validation prevents delete
I am trying to add validation to my User model:
class User < ActiveRecord::Base
validates :first_name, :last_name, :email, :password, presence: true
When the validation is in place, it prevents me from being able to
"delete"... I'm soft deleting users by setting an is_delete field to 1. I
suspect this is related to the fact that I don't actually store a
:password. Instead, I have callbacks that salt and hash the entered
password and save them into those respective fields (hashed_password &
salt).
If I try to validate those, it prevents creation:
class User < ActiveRecord::Base
validates :first_name, :last_name, :email, :hashed_pasword, :sal,
presence: true
This makes sense because they aren't present when the form is submitted.
How do I solve this?
Update In the controller...
def delete_user
user = User.find( params[:id] )
# if !user
# flash[:error] = "Problem deleting user"
# redirect_to controller:'admin', action:'index'
# end
if ( user.update( is_deleted: 1) )
flash[:notice] = "User successfully deleted"
else
flash[:error] = "Problem deleting user"
end
redirect_to controller:'admin', action:'index'
end
No comments:
Post a Comment