YAML文件有语法错误,但看起来它的格式与另一个有效的格式相同?

时间:2016-07-25 14:02:37

标签: ruby-on-rails yaml

我有两个格式相似的YAML文件,但是当我在rails应用程序上运行集成测试时,一个正在运行而另一个没有运行。

这是第一个不起作用的YAML文件:

prince_edward_island:
  name: 'prince edward island'
  country: 'Canada'

nova_scotia:       名称:' nova s​​cotia'       国家:'加拿大'

newfoundland:
  name: 'newfoundland'
  country: 'Canada'

new_brunswick:
  name: 'new brunswick'
  country: 'Canada'

quebec:
  name: 'quebec'
  country: 'Canada'

manitoba:
  name: 'manitoba'
  country: 'Canada'

alberta:
  name: 'alberta'
  country: 'Canada'

ontario:
  name: 'ontario'
  country: 'Canada'

british_columbia: 'Canada'
  name: 'british columbia'
  country: 'Canada'

saskatchewan: 'Canada'
  name: 'saskatchewan'
  country: 'Canada'

这是第二个可行的YAML文件:

student_advisor_message:
  body: 'This is a message'
  subject: 'Subject!'
  user: student_advisor

manager_message:
  body: 'This is a message'
  subject: 'Subject!'
  user: manager

read_manager_message:
  body: 'This is a message'
  subject: 'Subject!'
  user: manager

agent_manager_message:
  body: 'This is a message'
  subject: 'Subject!'
  user: agent_manager

message_for_jack:
  body: 'This is a message'
  subject: 'Subject!'
  user: manager

message_for_jack_sent:
  body: 'This is a sent message'
  subject: 'Sent Subject!'
  user: manager

message_for_jack_trash:
  body: 'This is a trashed message'
  subject: 'Trash Subject!'
  user: manager

错误是:

ERROR:

while parsing a block mapping
  in "<unicode string>", line 1, column 1:
    prince_edward_island:
    ^
expected <block end>, but found '<block mapping start>'
  in "<unicode string>", line 34, column 3:
      name: 'british columbia'

有人可以解释我两者之间缺少的差异吗?对于我而言,我使用空格而不是制表符。

2 个答案:

答案 0 :(得分:0)

yaml文件中的父级不能有值。

从这些行中删除(或评论)Canada时,它会正确解析。

british_columbia: 'Canada'
  name: 'british columbia'
  country: 'Canada'

saskatchewan: #'Canada'
  name: 'saskatchewan'
  country: 'Canada'

答案 1 :(得分:0)

有几个错误:

  

对于我使用制表符而不是空格。

YAML中不允许使用标签作为缩进字符。

country: 'Canada'

  nova_scotia:

在这里,您将标量('Canada')和映射(以nova_scotia:开头)作为键country的值。此密钥可能只有一个值。您必须删除标量'Canada'或将其包含的映射移动到其他位置。

british_columbia: 'Canada'
  name: 'british columbia'
  country: 'Canada'

saskatchewan: 'Canada'
  name: 'saskatchewan'
  country: 'Canada'

在这里,你再次犯同样的错误两次。