A
A
Anton Misyagin2021-06-30 19:13:19
Ruby on Rails
Anton Misyagin, 2021-06-30 19:13:19

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


In models model_1, model_2,... model_N
class Model_i < ApplicationRecord
include Imagable
end


In config/locales language files : lang_1, lang_2, ... lang_K
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"


With N and K 10 and 3 handmade somehow already dofiga.
I want something like:
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:
...


I don't know this chip and is there one? But agree, it would be convenient

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Misyagin, 2021-06-30
@sunnmas

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 question

Ask a Question

731 491 924 answers to any question