Extra Ruby lines inside. Should statements cause problems in Haml?

I'm trying to put some (non-rendering) comments inside an If / Else statement in one of my Haml views, but it seems to be causing problems.

I would like to have the following code:

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

Unfortunately, Rails raises this error:

 Got "else" with no preceding "if" 

If I remove the comment 'else', i.e.

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

Everything works as intended. The problem is not the comment itself. I need to remove the actual Ruby line of code (including the hyphen) in order to get it for rendering. That is, even if I just leave an empty line preceded by a hyphen, for example:

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

I get the same error. Other potentially relevant details: now there is more code that is at the same level of indentation as the if / else statement (not inside it), and all this is nested inside the form. Can someone explain to me what is going wrong? Thank you very much!

PS This is my first SO question, so if I presented it improperly, let me know.

+6
source share
1 answer

HAML Link:

Ruby blocks, such as XHTML tags, should not be explicitly closed in Haml. Rather, theyre automatically closing based on the indent. The block starts whenever the indentation increases after the Ruby calculation command. It ends when the indentation is reduced (until it is an else clause or something similar).

So, when you reduce indentation, and this line is not an else clause (or similar, elsif ), then if ends, end added implicitly. Then of course the else line is invalid

Your solution is to postpone the comment before or after the else clause:

 - if @condition - # Stuff like ______ activates the if statement (Some code) - else - # Stuff like _____ activates the else statement (Some other code) 
+9
source

Source: https://habr.com/ru/post/947957/


All Articles