如何使用Rails

时间:2016-01-07 09:42:00

标签: ruby-on-rails yaml

我正在尝试使用Frozen Record和YAML文件将一些问题加载到我的问题模型中。由于某种原因,格式化是关闭的,我无法弄清楚我做错了什么。目的是FrozenRecord将上传我的/config/initializers/questions.yml文件并创建三个新的Question记录,其参数如下:

问题模型:

class Question < FrozenRecord::Base
 include ActiveModel::Validations
 validates :next_question_id_yes, :question_text, :answer_type, presence: true
 self.base_path = 'config/initializers/'
end

Questions.yml文件:

questions:
- id: 1
    question_text: Why does this company need to buy your products or services?
    answer_type: Text Field
    next_question_id_yes: 2
    next_question_id_no: ~
- id: 2
    question_text: When do they need to have completed the project?
    answer_type: Datetime
    next_question_id_yes: 3
    next_question_id_no: ~
- id: 3
    question_text: What happens if they miss this deadline?
    answer_type: Text Field
    next_question_id_yes: 4
    next_question_id_no: ~

我收到以下错误:

Psych::SyntaxError: (config/initializers/questions.yml): mapping values are not allowed in this context at line 3 column 18

所以可能在第3行出了点问题,但我不知道它可能是什么。我试过在字符串周围添加“”,但这没有帮助。我似乎无法找到任何告诉我如何将整个对象格式化为YAML并上传到DB的结果(仅使用to_yaml方法将对象拉出为YAML的结果)。

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:3)

YAML文件格式正确:

- id: 1
  question_text: Why does this company need to buy your products or services?
  answer_type: Text Field
  next_question_id_yes: 2
  next_question_id_no: ~
- id: 2
  question_text: When do they need to have completed the project?
  answer_type: Datetime
  next_question_id_yes: 3
  next_question_id_no: ~
- id: 3
  question_text: What happens if they miss this deadline?
  answer_type: Text Field
  next_question_id_yes: 4
  next_question_id_no: ~