加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 内部的额外Ruby行如果语句在Haml中引起问题?

发布时间:2020-12-16 23:03:05 所属栏目:百科 来源:网络整理
导读:我试图在我的一个Haml视图中的一个If / Else语句中放入一些(非渲染的)注释,但它似乎导致了问题. 我想要以下代码: - # Stuff like ______ activates the if statement- if @condition (Some code)- # Stuff like _____ activates the else statement- else (
我试图在我的一个Haml视图中的一个If / Else语句中放入一些(非渲染的)注释,但它似乎导致了问题.

我想要以下代码:

- # Stuff like ______ activates the if statement
- if @condition
  (Some code)

- # Stuff like _____ activates the else statement
- else
  (Some other code)

不幸的是,Rails抛出了这个错误:

Got "else" with no preceding "if"

如果我删除’其他’评论,即

- # Stuff like ______ activates the if statement
- if @condition
  (Some code)

- else
  (Some other code)

一切都按预期工作.问题不在于评论本身.我必须删除实际的Ruby代码行(包括连字符)以使其呈现.也就是说,即使我只留下一个前面带有连字符的空行,如下所示:

- # Stuff like ______ activates the if statement
- if @condition
  (Some code)

-
- else
  (Some other code)

我犯了同样的错误.其他可能相关的细节:稍后有更多的代码与if / else语句(不在其中)相同的缩进级别,并且整个事物嵌套在表单中.有人可以向我解释出现了什么问题吗?非常感谢!

附:这是我的第一个问题,所以如果我不恰当地提出这个问题,请告诉我.

解决方法

HAML reference says:

Ruby blocks,like XHTML tags,don’t need to be explicitly closed in Haml. Rather,they’re automatically closed,based on indentation. A block begins whenever the indentation is increased after a Ruby evaluation command. It ends when the indentation decreases (as long as it’s not an else clause or something similar).

因此,当您减少缩进,并且该行不是else子句(或类似的,例如elsif)时,if结束 – 隐式添加结束.当然,else行无效

您的解决方案是在else子句之前或之后缩进注释:

- if @condition
  - # Stuff like ______ activates the if statement
  (Some code)

- else
  - # Stuff like _____ activates the else statement
  (Some other code)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读