带有erb的YAML无法解析 [英] YAML with erb is not parsing
本文介绍了带有erb的YAML无法解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么这个yaml文件无法解析?
---
<%sensor_types = YAML。 load_file('db / seed-fixtures / sensor_type.yml')%>
<%sensor_types.each做| sensor_type | %>
sensor<%= sensor_type [’id’]%> ;:
id:<%= sensor_type [’id’]%>
标题:<%= sensor_type [’title’]%>
单位:<%= sensor_type [’unit’]%>
valid_min:<%= sensor_type [’valid_min’]%>
valid_max:<%= sensor_type [’valid_max’]%>
代号:<%= sensor_type [’代号’]%>
scale_base_ten_exponent:<%= sensor_type ['scale_base_ten_exponent']%>
<%end%>
此文件用于我的测试中的夹具,由rspec从夹具目录中加载。 / p>
当我尝试时得到:在这种情况下,第4行第28列(Psych :: SyntaxError)不允许映射值
解决方案
您无法像基本YAML文件一样加载包含ERB的YAML文件。查看此帖子。
相反,您可以做的是(在规范初始化程序或before钩子中):
FIXTURE_CONFIG = YAML。 load(ERB.new(File.read(#{Rails.root} /path_to_your_file.yml.erb))。result)
然后在测试中使用此变量。
Why this yaml file won't parse?
---
<% sensor_types = YAML.load_file('db/seed-fixtures/sensor_type.yml') %>
<% sensor_types.each do |sensor_type| %>
sensor<%= sensor_type['id'] %>:
id: <%= sensor_type['id'] %>
title: <%= sensor_type['title'] %>
unit: "<%= sensor_type['unit'] %>"
valid_min: <%= sensor_type['valid_min'] %>
valid_max: <%= sensor_type['valid_max'] %>
codename: <%= sensor_type['codename'] %>
scale_base_ten_exponent: <%= sensor_type['scale_base_ten_exponent'] %>
<% end %>
this file is used for fixtures in my tests, it is loaded by rspec from the fixtures directory.
when I try it I get: "mapping values are not allowed in this context at line 4 column 28 (Psych::SyntaxError)"
解决方案
You can't load a YAML file containing ERB like a basic YAML file. Checkout this post.
What you can do instead is (in a spec initializer or a before hook):
FIXTURE_CONFIG = YAML.load(ERB.new(File.read("#{Rails.root}/path_to_your_file.yml.erb")).result)
And then use this variable in your test.
这篇关于带有erb的YAML无法解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文