[英]Rails load yml file in application
I have made a yml file and placed in config/locals/models/en-US.yml .But I don't know how to load that file and get the data from that file to a view我制作了一个 yml 文件并放在 config/locals/models/en-US.yml 中。但我不知道如何加载该文件并将该文件中的数据获取到视图
en-US.yml en-US.yml
en-US:
mysqlid:
models:
dashboard:
one: Dashboard
other: Dashboards
attributes:
dashboard:
email: Email
name: Name
facebook_verified: Verified on Faceboo
Add this to your config/application.rb
file:将此添加到您的
config/application.rb
文件中:
config.i18n.load_path +=
Dir[Rails.root.join('config', 'locales', '**/*.{rb,yml}').to_s]
More info there: http://guides.rubyonrails.org/i18n.html更多信息:http: //guides.rubyonrails.org/i18n.html
To get your yml data, you will need to use the YAML
class to load the file.要获取 yml 数据,您需要使用
YAML
类来加载文件。
To do this, you have to require yaml
and then use it to load the data like below:为此,您必须要求
yaml
,然后使用它来加载数据,如下所示:
#controller:
require 'yaml'
def method
@yaml_data = YAML.load_file('config/locales/models/en-US.yml ')
end
and the yaml
data will be loaded into the @yaml_data
instance variable, which you can now access from your view.并且
yaml
数据将被加载到@yaml_data
实例变量中,您现在可以从视图中访问该变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.