2012-02-18 50 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