Вот моя учетная модель:
class Credential < ApplicationRecord
validate :password_or_certificate
enum credential_type: { windows: 1, linux: 2 }
def password_or_certificate
unless user_pass.blank? ^ cert_file_path.blank?
errors.add(:base, "Please provide a password or a certificate, not both.")
end
end
end
Я проверяю сертификат в контроллере и устанавливаю ошибку в контроллере следующим образом:
def create
attachment = params[:credential][:cert_file_path]
cert_file_path = Rails.root.join('private', 'certificates', attachment.original_filename) if attachment.present?
@credential = Credential.new(credential_params)
@credential.cert_file_path = cert_file_path
if @credential.valid? && cert_file_path.present?
cert_error_msg = 'Certificate is not valid'
fm = FileMagic.new(FileMagic::MAGIC_MIME)
file_path = attachment.path
if fm.file(file_path) =~ /^text\//
puts first_line = File.open(attachment.path) { |f| f.readline }
if first_line.include? '-----BEGIN RSA PRIVATE KEY-----'
File.open(cert_file_path, 'w') { |f| f.write(attachment.read) }
else
@credential.errors.add(:cert_file_path, cert_error_msg)
end
else
@credential.errors.add(:cert_file_path, cert_error_msg)
end
end
respond_to do |format|
if @credential.save
format.html { redirect_to credentials_url, notice: 'Credential was successfully mapped.' }
format.js
format.json { render :show, status: :created, location: @credential }
else
format.html { render :new }
format.js
format.json { render json: @credential.errors, status: :unprocessable_entity }
end
end
end
Даже если ошибки устанавливаются, запись сохраняется.
#<ActiveModel::Errors:0x0000557781babde8 @base=#<Credential id: nil, alias: "My new credential", user_name: "raj", encrypted_user_pass: "", encrypted_user_pass_iv: "VrT0xsxYtf//cwVx\n", credential_type: "linux", created_at: nil, updated_at: nil, cert_file_path: "/home/rmishra/awsapp/private/certificates/Ruby Enc...", passphrase: "">, @messages={:cert_file_path=>["Certificate is not valid"]}, @details={:cert_file_path=>[{:error=>"Certificate is not valid"}]}>
Я знаю, что могу проверить @credential.errors.blank?
и затем сохранить его, вы, ребята, поможете мне внедрить всю эту логику в мою модель.