Q. Is there an easy way to comment multiple line of ruby in an html.erb file when you have:
... <% .. %> blah <% .. %> blah <% .. %> blah ...
I tried to put <!– –> around it but its still interpreted.
A. There are a few ways. First, you could add a -# at the beginning of the erb tags like this:
<%-# whatever erb code -%>
Secondly, you could do an if false statement as explain here http://blog.localkinegrinds.com/2008/05/02/erb-block-comments-in-rhtml-templates-using-ruby-on-rails like this:
<% if false %><!-- start erb comment --> ... <% .. %> blah <% .. %> blah <% .. %> blah ... <!-- end erb comment --><% end %>
This will color code that portion as a comment in text editors and will prevent it from getting send to the browsers at all.
--------------------------------------------------------------------------------------------------------------------------------------------
ERB hints
Technical documentation
30 April 2008
ERB is the templating language you are using when you create view files in Rails. The view files will be usually named action.html.erb (or action.rhtml if you are still using Rails 1.2).
The files are processed by the ERB template engine, which looks for tags like this: <% %>
.
ERB Tag format | Used for | Example |
<% %> | Executing some ruby code | <% if User.valid? %> |
<%# %> | Commenting out some code | <%# comment here... %> |
<%= %> | Inserting the result of running some ruby code | <%= Time.now %> |
<%% | If you want the literal characters <% to be inserted in the page | <%% |
With most of the tags, you can change how ERB trims white space on the same line. Basically the addition of a "-" character to the end of a tag will prevent ERB from including any white space after the tag on the same line to the HTML, including the final carriage return. A "-" at the start of the tag does the same for characters before the tag.
That's quite hard to explain without some examples:
Ruby Code tags (<%
)¶
1 No hyphens, there will be a blank line below
2 containing the two spaces from the beginning of the line
3 <% %>
4 Last line
Note: the Textile formatter on this website has removed the two spaces from the blank line, but if you try this example yourself using Rails the spaces will be there.
No hyphens, there will be a blank line below containing the two spaces from the beginning of the line <-(two spaces here) Last line
1 Hyphen at the end of the tag, just the two spaces
2 before the tag on the line below will be left
3 <% -%>
4 Last line
Hyphen at the end of the tag, just the two spaces before the tag on the line below will be left Last line
1 Hyphen at both ends of the tag, all white space
2 is removed from the line containing the tag
3 <%- -%>
4 Last line
Hyphen at both ends of the tag, all white space is removed from the line containing the tag Last line
1 This only works if there is only white space before or after the tag
2 1 <%- -%> 2
3 Last line
This only works if there is only white space before or after the tag 1 2 Last line
Ruby Expression tags (<%=
)¶
1 Insert the current time here:
2 <%= Time.now.to_s(:db) %>
3 Last line
Insert the current time here: 2008-04-30 11:48:26 Last line
1 Insert the current time here:
2 <%= Time.now.to_s(:db) -%>
3 Last line
Insert the current time here: 2008-04-30 11:48:26 Last line
Double-percent (<%%
)¶
1 Double-percent test
2 <%= Time.now %>
Double-percent test Wed Apr 30 11:48:26 +0100 2008
1 Percent test 2
2 <%%= Time.now %>
Percent test 2 <%= Time.now %>
Comments (<%#
)¶
1 Comment test 1
2 <%# comment %>
3 Last line
Note: the Textile formatter on this website has removed the two spaces from the blank line, but if you try this example yourself using Rails the spaces will be there.
Comment test 1 Last line
Trimming (<%-
or -%>
)¶
1 Trimming test
2 <% 3.times do %>
3 Hello
4 <% end %>
5 Last line
Trimming test Hello Hello Hello Last line
1 Trimming test 2
2 <%- 3.times do -%>
3 Hello
4 <%- end -%>
5 Last line
Trimming test 2 Hello Hello Hello Last line
Trimming comments (<%-# -%>
)¶
1 End trim
2 <%# comment -%>
3 Last line
End trim Last line
1 Start and end trim
2 <%-# comment -%>
3 Last line
Start and end trim Last line