Am a newbie to rails , please let me know the way to comment out a single line and also to comment out a block of lines in *.html.erb files.
-
1possible duplicate of [Block comments in html.erb templates in rails](http://stackoverflow.com/questions/3127644/block-comments-in-html-erb-templates-in-rails) for blocks, http://stackoverflow.com/questions/2774841/best-way-to-add-comments-in-erb for single lines – Ciro Santilli OurBigBook.com Sep 01 '14 at 21:43
3 Answers
ruby on rails notes has a very nice blogpost about commenting in erb-files
the short version is
to comment a single line use
<%# commented line %>
to comment a whole block use a if false
to surrond your code like this
<% if false %>
code to comment
<% end %>

- 55
- 1
- 8

- 19,708
- 3
- 45
- 61
-
15+1 For solving my issue, but I've found it very ugly. To comment one line I must use 3 additional characters, and the block comment is nothing but code that will be not executed - no other color coding that makes it very unpractical to see which code is not executed on first look. – gotqn Nov 05 '12 at 18:34
-
24
-
A comment_block helper function would be awesome. Maybe when I gain a little more experience with Rails (I'm pretty new), I will create a pull request – Daniel Waltrip Oct 16 '13 at 07:26
-
4@gotqn Then you will LOVE [HAML](http://haml.info/docs/yardoc/file.REFERENCE.html#haml_comments_)! – Chloe Apr 11 '14 at 18:38
Note that if you want to comment out a single line of printing erb you should do like this
<%#= ["Buck", "Papandreou"].join(" you ") %>
This is CLEANEST, SIMPLEST ANSWER for CONTIGUOUS NON-PRINTING Ruby Code:
The below also happens to answer the Original Poster's question without, the "ugly" conditional code that some commenters have mentioned.
CONTIGUOUS NON-PRINTING Ruby Code
This will work in any mixed language Rails View file, e.g,
*.html.erb, *.js.erb, *.rhtml
, etc.This should also work with STD OUT/printing code, e.g.
<%#= f.label :title %>
DETAILS:
Rather than use rails brackets on each line and commenting in front of each starting bracket as we usually do like this:
<%# if flash[:myErrors] %> <%# if flash[:myErrors].any? %> <%# if @post.id.nil? %> <%# if @myPost!=-1 %> <%# @post = @myPost %> <%# else %> <%# @post = Post.new %> <%# end %> <%# end %> <%# end %> <%# end %>
YOU CAN INSTEAD add only one comment (hashmark/poundsign) to the first open Rails bracket if you write your code as one large block... LIKE THIS:
<%# if flash[:myErrors] then if flash[:myErrors].any? then if @post.id.nil? then if @myPost!=-1 then @post = @myPost else @post = Post.new end end end end %>

- 2,193
- 4
- 30
- 57
-
@TejasKale sorry... not sure what you mean? --> the chances of **YOUR** code or **MY** code** working? – Flak DiNenno Apr 10 '14 at 18:45
-
your method above would generate errors if the code is incorrect, which will almost always be the case with me :) – Tejas Kale Apr 11 '14 at 06:32
-
first off, no need for the `then`. Second, this completely destroy the idea behind `MVC`. Keep your logic in a helper or in your controller. -1 – davegson Nov 08 '14 at 09:59
-
@TheChamp I do appreciate you stating why you are downgrading; that's helpful for everyone. But, you are wrong: my answer, adequately addresses the question, and *is* correct. If you have a BETTER way of doing it, e.g. without the use of `then`, then you should add your own answer. Beyond that, the OP does **not** ask about MVC theory, or the Rails paradigm. Finally, if you do believe it is a *BAD QUESTION* (because commenting out logic breaks Rails MVC best practices) then you should downvote the **QUESTION** not my answer... – Flak DiNenno Nov 10 '14 at 12:33
-
@TomLord with respect, yes you are missing something... the `if` statement is irrelevant.. I am simply using it as an example of a block of code that he might want to comment out, and the use of an OPEN comment tag at the beginning and a CLOSE comment tag at the end – Flak DiNenno Dec 07 '16 at 12:32