0

I am making config/locales/custom_error.yml and loading it in config/initializer/app_errors.rb. It has the following contents:

custom_error.yml

app_flow:
  errors:
    fill_important_fields: "We recommend you to Fill this form "

app_errors.rb

APP_ERROR = YAML.load_file("#{Rails.root}/config/locales/custom_error.yml")

Now in my application I can access keys like APP_ERROR['app_flow']['errors']['fill_important_fields'] - I read This Question but not the exact same as I'm asking .

My Question

I want to access these nested keys something like class methods / members like . app_error.errors.fill_important_fields

Community
  • 1
  • 1
Mani
  • 2,391
  • 5
  • 37
  • 81

1 Answers1

0

You can convert your hash to a Super-Hash! (with this light gem: https://rubygems.org/gems/shash/versions/0.0.7)

APP_ERROR = YAML.load_file("#{Rails.root}/config/locales/custom_error.yml")
APP_ERROR = APP_ERROR.to_shash

Example:

sa = { first_key: { key_nested: 'value' } }.to_shash
# => #<Shash:0x000000099d0018 @hash={:first_key=>#<Shash:0x000000099cffa0 @hash={:key_nested=>"value"}>}> 
sa.first_key.key_nested
# => "value" 
MrYoshiji
  • 54,334
  • 13
  • 124
  • 117