我试图仅在存储.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我也无法存储它,我做错了吗?