014. rails 5 throw :abort @user.errors[:base] - cwy007/tips-and-skills GitHub Wiki

You can use errors.add for your class method.

User model:

def destroy_validation
  if some_condition
    errors.add(:base, "can't be destroyed cause x,y or z")
    throw(:abort)
  end
end

Users controller:

def destroy
  if @user.destroy
    respond_to do |format|
      format.html { redirect_to users_path, notice: ':)' }
      format.json { head :no_content }
    end
  else
    respond_to do |format|
      format.html { redirect_to users_path, alert: ":( #{@user.errors[:base]}"}
    end
  end
end

reference link: https://stackoverflow.com/questions/38625276/rails-5-throw-abort-how-do-i-setup-error-messages