Saw*_*ath 6 ruby haml ruby-on-rails
我试图在我的一个Haml视图中的一个If/Else语句中放入一些(非渲染的)注释,但它似乎导致了问题.
我想要以下代码:
- # Stuff like ______ activates the if statement
- if @condition
(Some code)
- # Stuff like _____ activates the else statement
- else
(Some other code)
Run Code Online (Sandbox Code Playgroud)
不幸的是,Rails抛出了这个错误:
Got "else" with no preceding "if"
Run Code Online (Sandbox Code Playgroud)
如果我删除'else'评论,即
- # Stuff like ______ activates the if statement
- if @condition
(Some code)
- else
(Some other code)
Run Code Online (Sandbox Code Playgroud)
一切都按预期工作.问题不在于评论本身.我必须删除实际的Ruby代码行(包括连字符)以使其呈现.也就是说,即使我只留下一个前面带有连字符的空行,如下所示:
- # Stuff like ______ activates the if statement
- if @condition
(Some code)
-
- else
(Some other code)
Run Code Online (Sandbox Code Playgroud)
我犯了同样的错误.其他可能相关的细节:稍后有更多的代码与if/else语句(不在其中)相同的缩进级别,并且整个事物嵌套在表单中.有人可以向我解释出现了什么问题吗?非常感谢!
PS这是我的第一个问题,所以如果我不恰当地提出这个问题,请告诉我.
该HAML参考说:
Ruby块(如XHTML标记)不需要在Haml中显式关闭.相反,它们会根据缩进自动关闭.每当Ruby评估命令后缩进增加时,块就会开始.它在缩进减少时结束(只要它不是else子句或类似的东西).
因此,当您减少缩进,并且该行不是else子句(elsif例如,类似)时,将if完成end隐式添加完成 - .那么当然这else条线是无效的
您的解决方案是在else条款之前或之后缩进注释:
- if @condition
- # Stuff like ______ activates the if statement
(Some code)
- else
- # Stuff like _____ activates the else statement
(Some other code)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1891 次 |
| 最近记录: |