将YAML键作为对象或类成员变量访问

时间:2016-07-20 20:22:18

标签: ruby-on-rails ruby-on-rails-4 yaml

我正在制作config/locales/custom_error.yml并将其加载到config/initializer/app_errors.rb。它具有以下内容:

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")

现在在我的应用程序中,我可以访问APP_ERROR['app_flow']['errors']['fill_important_fields']之类的密钥 - 我读了This Question但不是我要求的完全相同。

我的问题

我想访问类似class methods / members之类的嵌套密钥。 app_error.errors.fill_important_fields

1 个答案:

答案 0 :(得分:0)

您可以将哈希转换为超级哈希! (用这个光宝石:https://rubygems.org/gems/shash/versions/0.0.7

 work.jobs.forEach(function(job) {
    console.log(job);
    $("#workExperience").append(HTMLworkStart);


    formattedEmployer = HTMLworkEmployer.replace("%data%",job.employer);
    formattedTitle = HTMLworkTitle.replace("%data%",job.title);
    var formattedEmployerTitle = formattedEmployer + formattedTitle;
    $(".work-entry:last").append(formattedEmployerTitle);

    formattedworkLocation = HTMLworkLocation.replace("%data%",job.location);
    $(".work-entry:last").append(formattedworkLocation);
    formattedworkDates = HTMLworkDates.replace("%data%",job.dates);
    $(".work-entry:last").append(formattedworkDates);
    formattedworkDescrip = HTMLworkDescription.replace("%data%",job.description);
    $(".work-entry:last").append(formattedworkDescrip);


});

示例:

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