带有 erb 的 YAML 未解析 [英] YAML with erb is not parsing
本文介绍了带有 erb 的 YAML 未解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么这个 yaml 文件不会解析?
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 %>
这个文件用于我的测试中的fixtures,它是由rspec从fixtures目录加载的.
this file is used for fixtures in my tests, it is loaded by rspec from the fixtures directory.
当我尝试它时,我得到:在第 4 行第 28 列 (Psych::SyntaxError) 的上下文中不允许映射值"
when I try it I get: "mapping values are not allowed in this context at line 4 column 28 (Psych::SyntaxError)"
推荐答案
您不能像基本 YAML 文件那样加载包含 ERB 的 YAML 文件.查看此帖子.
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屋!
查看全文