【问题标题】:Haml multiline comments and if-elsif-else statementHaml 多行注释和 if-elsif-else 语句
【发布时间】:2015-01-16 00:55:17
【问题描述】:

我有以下代码。

- if specializations.count <= 0
  .alert.alert-warning
    Warning message

-# - elsif agency.offers_limit >= agency.offers.count
-#   .alert.alert-warning
-#     Warning message

- else
  = render 'form'

在这种情况下,我收到Got "else" with no preceding "if"。我还能如何评论部分已评论的代码,以便正确处理- else ...

如果我这样评论代码:

- if specializations.count <= 0
  .alert.alert-warning
    Warning message

-# - elsif agency.offers_limit >= agency.offers.count
  .alert.alert-warning
    Warning message

- else
  = render 'form'

我没有错误,但- else ... 没有被处理,也没有呈现表单。

【问题讨论】:

  • 我认为您发布的代码没有任何问题。 HAML 使用nested comments,因此您在此处发布的两个代码段是相等的。也许还有其他事情发生。我建议您在使用的任何文本编辑器中为 HAML 进行语法高亮显示。
  • 语法高亮一切正常。我要评论的那个块 - 返回 TRUE,所以如果存在就应该处理它。如果我评论它,我希望它被跳过,因此结果 - 应该呈现表单。但是只有在我删除注释块时才会呈现表单。
  • 好的,所以它可能仍在处理中。尝试删除多余的连字符,使其看起来像-# elsif agency.offers_limit &gt;= agency.offers.count,看看它是否有效。
  • 不,这没有帮助 - 一切都被忽略了。看来我刚刚找到了解决方案 - 将在几分钟后作为答案发布。
  • 这实际上看起来像是 Haml 解析器中的一个错误。我在这里打开了一个错误报告:github.com/haml/haml/issues/815

标签: ruby-on-rails comments haml


【解决方案1】:

不应在if - elsif - else 语句中处理的代码应缩进。所以,如果我想跳过elsif 语句,我的代码应该是这样的:

- if specializations.count <= 0
  .alert.alert-warning
    Warning message 

  -# - elsif agency.offers_limit >= agency.offers.count
  -#  .alert.alert-warning
  -#    Warning message

- else
  = render 'form'

或像这样(以保持正确的缩进):

- if specializations.count <= 0
  .alert.alert-warning
    Warning message 

  -# - elsif agency.offers_limit >= agency.offers.count
    .alert.alert-warning
      Warning message

- else
  = render 'form'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-28
    相关资源
    最近更新 更多