问题
I am new to haml and this is stumping me. I don't like to delete code where I can comment it out but I have no idea how to properly end a comment in haml.
Here is a code snippit:
.field
= f.label :member_id
%br/
= f.text_field :member_id
.field
= f.label :instrument_type
%br/
I'm trying to comment out the first field so I used:
/
.field
= f.label :member_id
%br/
= f.text_field :member_id
.field
= f.label :instrument_type
%br/
but that commented out everything after the first field.
Then I tried:
/
.field
= f.label :member_id
%br/
= f.text_field :member_id
.field
= f.label :instrument_type
%br/
but it didn't like that either, or:
-#.field
-# = f.label :member_id
-# %br/
-# = f.text_field :member_id
.field
= f.label :instrument_type
%br/
I must be missing something. I looked all over but the examples never show code after the comment.
回答1:
It's your spacing that is causing the problem, not your method. Here is the proper way to comment out those lines in HAML:
Your 4th example is really close:
-#.field
-# = f.label :member_id
-# %br/
-# = f.text_field :member_id
.field
= f.label :instrument_type
%br/
Commented out properly:
-#.field
-# = f.label :member_id
-# %br
-# = f.text_field :member_id
.field
= f.label :instrument_type
%br
This is awfully close to what you posted on your last example, with a notable exception: Your comment lines begin with a space preceding the -#
. That space at the beginning will break HAML. I also noticed that your source code is indenting by one space instead of two. This will also break HAML. It must be two spaces of indentation.
P.S. You can remove the trailing slash from your %br
lines.
回答2:
A /
on a blank line, followed by code, comments an indented section of code which is probably why your entire section of code is getting commented out.
Try commenting each line.
/.field<br>
/ = f.label :member_id<br>
/ %br/<br>
/ = f.text_field :member_id<br>
.field<br>
= f.label :instrument_type<br>
%br/<br>
Or, alternatively a blank line between the fields may give the comment a hint as to where it should end.
/
.field<br>
= f.label :member_id<br>
%br/<br>
= f.text_field :member_id<br>
.field<br>
= f.label :instrument_type<br>
%br/<br>
来源:https://stackoverflow.com/questions/6310761/ending-haml-comments