Search code examples
ruby-on-railsrails-i18n

Rails translate custom base error


My models:

Product has_many variants has_many sizes

To sizes I add custom error, like this:

errors.add :base, "My custom error msg"

In the view I see: "Sizes base My custom error msg"

class Size < ActiveRecord::Base
  ...
  validate :custom_error, only: :update
  ...
  def custom_error
    errors.add :base, "My custom error msg"
  end
end

But how translate this message?

UPDATED I am found solution:

In locale.yml:

attributes:
  variants/sizes:
    base: ''

Solution

  • Try:

    errors.add :base, :custom_error
    

    This should give you translation missing together with key name and a scope where this translation should be stored.

    Update:

    Just did it by myself:

    translation missing: [locale].activerecord.errors.models.[model_name].attributes.base.custom_error