Override nested attribute name with I18n

I have two models Store

and StoreDetail

as follows:

class Store
   has_one :store_detail, dependent: :destroy
end

      

And StoreDetail:

class StoreDetail
   belongs_to :store, class_name: 'Store'
   belongs_to :state, class_name: 'State'
   belongs_to :city, class_name: 'City'
   belongs_to :zip_code, class_name: 'Zip Code'
end

      

And I am overriding the state attribute like this:

attributes:
  store/store_detail:
    state: "State"
    city: "City"
    zip_code: "Zip Code"

      

But I got validation messages:

Store detail state can't be blank
Store detail city can't be blank
Store detail zip code can't be blank

      

I want to do it without "Store detail" like below:

State can't be blank

      

How can I override nested attributes?

+3


source to share


2 answers


You can use special validation messages:

class StoreDetail < ActiveRecord::Base
  validates :state_id, presence: true,
  message: I18n.t(:state_cant_be_blank)
end

      



And then in locales / *. yml

 en:
   state_cant_be_blank: State can't be blank

      

0


source


This is very possible with the i18n:

en:
  activerecord:
    errors:
      models:
        store_detail:
          attributes:
            state:
              blank: State can't be blank.
            city:
              blank: City can't be blank.
            zip_code:
              blank: Zip code cant't be blank.

      

Very important

  • All these nested tags starting with "en" and ending with "models" must be exactly as shown in the example.

  • This is important for the model name in en.yml (or whatever pencil file you are using)

  • The model name must match model.rb as this identifies the field names to be checked for activerecord

  • Validation field names must match schema.rb field names, such as zip_code, not zip.

  • The "blank:" identifier tells the activetsecord Rails component to replace what you have in the i18n yaml file when this particular check fails.

I18n debugging (added)



  • Validate YAML . If you don't see the en.yml validation message that you defined, but keeps showing by default, your en.yml isn't working for some reason. YAML is very picky about whitespace in the file and any trailing whitespace in the file, etc. But it doesn't show any errors, it just falls back to using the default Rails messages. Here's one YAML Lint tool: http://yaml-online-parser.appspot.com/

  • Rails specification for i18n : http://guides.rubyonrails.org/i18n.html .

  • Debugging i18n . Use g18 i18n tasks to generate a comprehnsive report of your i18n components: https://github.com/glebm/i18n-tasks

  • Application.rb : make sure you have i18n installed.

# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
  config.i18n.default_locale = :en
  config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')] 

      


If you want to create custom error messages like activerecord, you need to work through i18n. It's not a neat thing. It's not DRY at all, but it's what's available.

0


source







All Articles