ruby-on-rails - 使用 haml 显示错误消息

标签 ruby-on-rails ruby-on-rails-3 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

我不知道我做错了什么。

最佳答案

首先,您的 %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

关于ruby-on-rails - 使用 haml 显示错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9344025/

相关文章:

ruby-on-rails-3 - Rails 操作缓存过期后刷新

Rails View 模板中的 html.haml 与 haml

ruby-on-rails - 嵌套 Controller 中未初始化的常量错误

ruby-on-rails - 未初始化的常量 sidekiq worker

ruby-on-rails - bcrypt-ruby 无法构建 gem native 扩展

ruby-on-rails - 丑陋数据库的API

jquery - soundmanger 2 无法在 Rails 应用程序中工作

ruby - 多个变量合二为一 - Rails

javascript - 根据其位置更改导航栏颜色。颜色不变

css - 在 HAML 中启动有序列表