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
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"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.