我最大的愿望是在yml文件中使用gem faker。我知道这种方式是错误的,但是可以这样做吗?
请看我的代码:
-yml文件
:usuarios:
:ncpf: Faker::Number.number
:birth Faker::Date.birthday-我的页面(site-prism)
def new_user
cpf.set(DADOS[:users][:ncpf])
dt_birth.set(DADOS[:users][:birth])
end发布于 2021-01-23 21:31:21
开箱即用,这对于普通的YAML是不可能的。但是,当您在解析之前通过ERB运行您的YAML文件时,您可以这样做。
将YAML更改为
:users:
:ncpf: <%= Faker::Number.number %>
:birth: <%= Faker::Date.birthday %>然后像这样读这个文件
require 'erb'
require 'json'
file = File.read('path/filename.yml')
yaml = ERB.new(file).result
DADOS = YAML.load(yaml)顺便说一下,这就是Rails在内部对配置文件所做的事情。因此,当您使用Rails时,您可以使用这个简化版本来加载该文件
DADOS = ActiveSupport::ConfigurationFile.parse('path/filename.yml')https://stackoverflow.com/questions/65859270
复制相似问题