我试图仅对存储的 .yml 文件设置验证。通过一些研究,我正在这样做:
class TranslationFile < ApplicationRecord
has_one_attached :file
validate :only_yml_type
private
def only_yml_type
if file.attached? && !file.content_type.in?(%w(application/x-yaml))
file.purge
errors.add(:file, 'Must be a yaml file')
end
end
end
但是现在即使文件是 .yml / .yaml 我也无法存储它,我做错了什么吗?