ruby-on-rails – 内部的额外Ruby行如果语句在Haml中引起问题?
我试图在我的一个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:
因此,当您减少缩进,并且该行不是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) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |