Topic: Comments in views

Is it possible to comment out a part of a view without using SGML comments?

Re: Comments in views

<% =begin
this is a
multiline comment
=end %>
<h1>other stuff</h1>

vinnie - rails forum admin

Re: Comments in views

Thanks!

Re: Comments in views

Are you sure - this just gives a syntax error for me:

compile error
/home/ke/rails/shippingwork/config/../app/views/accounts/signup.rhtml:10: parse error, unexpected '=', expecting kEND
=begin
  ^
/home/ke/rails/shippingwork/config/../app/views/accounts/signup.rhtml:13: parse error, unexpected '=', expecting kEND
=end ; _erbout.concat "\n"
^
/home/ke/rails/shippingwork/config/../app/views/accounts/signup.rhtml:28: parse error, unexpected $, expecting kEND

Re: Comments in views

Try this:

<%
=begin %>
this is a
multiline comment
<%
=end %>

Railscasts - Free Ruby on Rails Screencasts

Re: Comments in views

Thanks, that worked. (Not that I know what it means though)

Re: Comments in views

See if this clears it up a bit. The syntax for a multiline comment in Ruby is:

=begin
multiline
comment
=end

This is just like putting # at the beginning of each line. To put ruby in an rhtml file you need to put it in erb <% %> tags.

<% =begin
multiline
comment
=end %>

I think the reason this is failing is because the =begin and =end must be at the beginning of the line. So it makes this:

<%
=begin
multiline
comment
=end %>

There is one more problem with that code. If you have <% %> tags in there (such as if you want to comment out code) it will cause an error. I'm assuming because erb is trying to interpret it and isn't smart enough to see that it is in a comment. To solve that you have to enclose the =begin and =end in their own erb tags. So we end up with this:

<%
=begin %>
multiline
<% some commented code %>
comment
<%
=end %>

Not very pretty, but it gets the job done.

Last edited by ryanb (2006-07-24 19:56:44)

Railscasts - Free Ruby on Rails Screencasts

Re: Comments in views

It also sometimes usefull to use html comments to see what code would actually be produced by ruby.  E.g.

<!-- <%= text_field 'foo', 'method' %> -->

I found that usefull, expecially when trying to sort out some autocompletion stuff

Re: Comments in views

It would be nice if we had comments like JSP <%-- --%>. sad

Re: Comments in views

There's always this:

<%# this is a comment %>

But that's not multiline.

Railscasts - Free Ruby on Rails Screencasts

11

Re: Comments in views

Catharsis wrote:

It also sometimes usefull to use html comments to see what code would actually be produced by ruby.  E.g.

<!-- <%= text_field 'foo', 'method' %> -->

I found that usefull, expecially when trying to sort out some autocompletion stuff

I guess it's okay for stuff like that, but if you want to know something like, say the value of a model's attribute, you're best off using the tools built into Rails like script/console.

vinnie - rails forum admin

Re: Comments in views

Waking up an old thread, but I get an error when trying this in a controller. What could be wrong?

syntax error, unexpected '=', expecting kEND
    =begin

13

Re: Comments in views

Are you sure you are putting =begin and =end on the beginning of lines, like ryanb described in Comment #7 of this topic?

Re: Comments in views

kek wrote:

Are you sure you are putting =begin and =end on the beginning of lines, like ryanb described in Comment #7 of this topic?

That's the answer. I missed that. I was thinking of "a new line". Thanks. smile

Re: Comments in views

ryanb wrote:

There's always this:

<%# this is a comment %>

But that's not multiline.

Waking up an old thread again. After I updated my Ruby from Version 1.8.6-p111 to 1.8.7-p72 I wondered about missing parts in my rendered HTML views. For example:

<div>
    <% if 1 + 1 == 2 %>
    Hello World!
    <% end # if 1 + 1 == 2 %>
</div>
Hello World!
<% %>
Hello World!

Everything is dropped after the rails-tag which the inline-comment upto the next rails-tag which may be empty. So the closing div-tag and the second "Hello World!" are missing. A line break after the inline-comment solves the problem but looks IMO horrible.

It seems that ruby does not recognize the closing rails-tag but there is no syntax error... I used different rails versions including the 2.3.2.

Does anybody share this problem? Has anyone a nice solution? Thanks! smile

16

Re: Comments in views

mabe, I can't repeat your problem. I get three times "Hello World!" with that code.

Re: Comments in views

It seems strange that rails wouldn't have a simple comment mechanism for views.

As stated above, <% rails code # comment %> does not work and I don't want to use HTML comments because I don't want certain comments exposed to the end user code. The =begin/=end method works but looks awful and makes it harder to read code. 

Furthermore, why should the # comment tag comment-out the %> pre-processor end tag? It looks like Rails is starting execution on the code (from the open tag) without first determining the end tag position and only executing on code WITHIN the tags.

Any other ideas for comments?

Re: Comments in views

I confirm that on Rails 2.3.5 with this code inside a .html.erb file:

<div>
    <% if 1 + 1 == 2 %>
    Text one
    <% end # if 1 + 1 == 2 %>
</div>
Text two
<% %>
Text three

the output generated is this:

<div>
    
    Text one
    
Text three

Does anyone know why this is happening? Really weird. I lost a few hours today on a bug of this kind.