2012-02-18 73 views
2

这里显示的错误消息是我的表格:与HAML

= form_for @talent do |f| 

    - if @talent.errors.any? 
    #error_explanation 
     %h2= pluralize(@talent.errors.count, "error") 
     "prohibited this user from being saved:" 
     %ul - @talent.errors.full_messages.each do |msg| 
      %li= msg 

    = f.label :First_Name 
    = f.text_field :first_name 
    = f.label :Last_Name 
    = f.text_field :last_name 
    = f.label :City 
    = f.text_field :city 
    = f.label :State 
    = f.text_field :state 
    = f.label :Zip_code 
    = f.text_field :zip_code 
    = f.label :Email 
    = f.text_field :email 
    = f.submit "Create" 

这是我的错误信息:

Illegal nesting: nesting within plain text is illegal. 

Extracted source (around line #7): 

4:  #error_explanation 
5:  %h2= pluralize(@talent.errors.count, "error") 
6:  "prohibited this user from being saved:" 
7:   %ul - @talent.errors.full_messages.each do |msg| 
8:   %li= msg 
9: 
10: = f.label :First_Name 

我想不出什么我做错了。

回答

3

首先,你的%ul行应该是自己的,并在它下面的循环。此外,您%ul线不应该被上面的字符串下缩进:

- if @talent.errors.any? 
    #error_explanation 
    %h2= pluralize(@talent.errors.count, "error") 
    "prohibited this user from being saved:" 
    %ul 
     - @talent.errors.full_messages.each do |msg| 
     %li= msg 

但你可能想要的是对于“禁止......”的部分是你h2的一部分吗?如果是这样的:

- if @talent.errors.any? 
    #error_explanation 
    %h2 
     = pluralize(@talent.errors.count, "error") 
     "prohibited this user from being saved:" 
    %ul 
     - @talent.errors.full_messages.each do |msg| 
     %li= msg 
2

如果你是把多个事物的标签内,你对你的h2,你需要缩进他们所有,而不是一个内联和添加第二个。 Haml认为你正试图将你的ul嵌套在它上面的文本中。

= form_for @talent do |f| 

    - if @talent.errors.any? 
    #error_explanation 
     %h2 
     = pluralize(@talent.errors.count, "error") 
     "prohibited this user from being saved:" 
     %ul - @talent.errors.full_messages.each do |msg| 
     %li= msg 

或者,如果你不想里面的文字h2你拿到的缩进你ul错误。将它带回两个空格。

#error_explanation 
    %h2= pluralize(@talent.errors.count, "error") 
    "prohibited this user from being saved:" 
    %ul - @talent.errors.full_messages.each do |msg| 
    %li= msg