Answer the question
In order to leave comments, you need to log in
How to remap active record error in localization file?
It is assumed that the project uses paperclip to upload photos to the site and I18n for internationalization. The system has N models and K languages for localization.
Next, let's say there is such a concern:
app/models/concerns/imagable.rb
module Imagable
extend ActiveSupport::Concern
included do
has_attached_file :image #в базе данных есть поле image, которое подвергается валидации
... many great methods are here
end
end
class Model_i < ApplicationRecord
include Imagable
end
lang_j:
active_record:
model_1:
attributes:
image_content_type:
invalid: "image content is invalid"
model_2:
attributes:
image_content_type:
invalid: "image content is invalid"
...
model_N:
attributes:
image_content_type:
invalid: "image content is invalid"
module Imagable
extend ActiveSupport::Concern
included do
...
active_record.error_messages.config.reassign field: :image, message: 'invalid_image_content_common'
end
end
lang_j:
active_record:
invalid_image_content_common: "image content is invalid"
model_1:
attributes:
...
Answer the question
In order to leave comments, you need to log in
config/initializers/attachment_content_type_validator.rb
class Paperclip::Validators::AttachmentContentTypeValidator
def mark_invalid(record, attribute, types)
record.errors.add(attribute, :invalid, message: I18n.t('paperclip.file_content_type_invalid'))
end
end
lang_j:
paperclip:
file_content_type_invalid: "File content invalid"
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question